document as child in DOM ??

  • Thread starter Juliano Freitas
  • Start date
J

Juliano Freitas

I have a document as a string:

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

then i transform the string into a document with fromString method.

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

<swdb>
<root>
<parara>text</parara>
</root>
....
</swdb>

i tryed the importNode and appendNode, but i dont if i'm doing right!

Juliano Freitas
 
U

Uche Ogbuji

Juliano Freitas said:
I have a document as a string:

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

then i transform the string into a document with fromString method.

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

<swdb>
<root>
<parara>text</parara>
</root>
...
</swdb>

i tryed the importNode and appendNode, but i dont if i'm doing right!

I think you want something like the following (pseudocode):

1) parse "root" document into root_dom
2) new_docelem = root_dom.createElementNS(None, 'swdb')
3) old_docelem = root_dom.documentElement
4) root_dom.documentElement = new_docelem
5) new_docelem.appendChild(old_docelem)

There. Now doesn't DOM just completely suck? :)

If this doesn't work, please post specific error messages or
unexpected results so we can hekp you better.

--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://4Suite.org http://fourthought.com
A hands-on introduction to ISO Schematron -
http://www-106.ibm.com/developerworks/edu/x-dw-xschematron-i.html
Schematron abstract patterns -
http://www.ibm.com/developerworks/xml/library/x-stron.html
Wrestling HTML (using Python) -
http://www.xml.com/pub/a/2004/09/08/pyxml.html
XML's growing pains - http://www.adtmag.com/article.asp?id=10196
XMLOpen and more XML Hacks -
http://www.ibm.com/developerworks/xml/library/x-think27.html
A survey of XML standards -
http://www-106.ibm.com/developerworks/xml/library/x-stand4/
 
A

Andrew Clover

[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 said:
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.
 
U

Uche Ogbuji

[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)

Good point. I was thinking Domlette, where we ditch DOM's sillier
rules (we're writing an API, not a Web browser) :)

--Uche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top