document as child in DOM ??

Andrew Clover and-google at doxdesk.com
Mon Nov 8 04:54:47 EST 2004


Uche Ogbuji <uche at ogbuji.net> wrote:

[on reparenting the root element]

> 4) root_dom.documentElement = new_docelem

Unfortunately documentElement is a readonly property, so this won't
fly. (minidom doesn't generate the expected NoModificationAllowedErr,
but it doesn't perform the replacement either.)

You'd have to say:

  root_dom.replaceChild(new_docelem, root_dom.documentElement)

Bizarrely, this isn't *guaranteed* to work; in DOM Level 2 Core it's
optional, and an implementation might respond with a NotSupportedErr.
However, minidom, 4DOM and pxdom seem quite happy with it.

[Well, now. It didn't work in Python 2.0's minidom. But then what
did?]

Juliano Freitas <jubafre at atlas.ucpel.tche.br> wrote:

> xmltext = """<?xml version='1.0'?>
> <root>
> 	<parara>text</parara>
> </root>"""

> i want tu put this "xmltext" as a child to another document called
> "swdb"?

Another way of doing it would be DOM Level 3 LS's parseWithContext
method:

  # doc is a Document containing an <swdb/> element
  # xmltext is as above

  parser= doc.implementation.createLSParser(1, None)
  input= doc.implementation.createLSInput()
  input.stringData= xmltext
  el= doc.documentElement
  parser.parseWithContext(input, el, parser.ACTION_APPEND_AS_CHILDREN)

LS isn't yet widely supported; pxdom will grok it but 4DOM won't.

-- 
Andrew Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/



More information about the Python-list mailing list