FAQ
I have 2 XML documents. I would like to copy the entire structure of one
document into a child element of the other, but i cannot copy the root node
across:
I get the following error from Zope:
Error Type: HierarchyRequestErr
Error Value: Node manipulation results in invalid parent/child relationship.

the structures are as follows:

<person>
<name/>
<address/>
<phone/>
</person>

and would like to copy this into the following format:

<xformimport>
<docID col="blah" id="blahblah"/>
<document>
<person>
<name/>
<address/>
<phone/>
</person>
</document>
</xformimport>

I have written all the code to build 'xformimport','docID' and 'document',
but just need to copy 'person' across.
I figure i may be missing an easy solution here, so any help/ideas will be
appreciated.
I am using PyXML 0.8.1 and python 2.1.3.

thanks
Jeremy

Search Discussions

  • Pat Notz at Sep 24, 2002 at 7:27 pm
    This is an untested snippet of code that should get you started. It
    looks like you'll want to do you .appendChild on a node other than
    .documentElement...

    # Merge subsequet DOMs
    dom = None
    for xmlfile in xmlfiles:
    tmp_dom = xml.dom.minidom.parse(xmlfile)
    if dom == None:
    dom = tmp_dom
    else:
    for node in tmp_dom.documentElement.childNodes:
    e = dom.importNode(node,True)
    dom.documentElement.appendChild(e)
  • Scott Yang at Sep 27, 2002 at 12:29 am
    Assuming you want to have the result DOM as
    <xformimport>
    <docID />
    <document />
    <person>
    <name />
    ...
    </person>
    </xformimport>

    You should be able to do it easily using PyXML.

    # Parse the XML document into DOM objects.
    from xml.dom.ext.reader.PyExpat import Reader
    person = Reader().fromString(doc1)
    xformimport = Reader().fromString(doc2)

    # Clone person's document node.
    person_cloned = person.cloneNode(1, xformimport)

    # Append cloned node to xformimport's child nodes.
    xformimport.documentElement.appendChild(person_cloned)


    In article <mailman.1032866191.25215.python-list at python.org>, Jeremy Rew wrote:
    I have 2 XML documents. I would like to copy the entire structure of one
    document into a child element of the other, but i cannot copy the root node
    across:
    I get the following error from Zope:
    Error Type: HierarchyRequestErr
    Error Value: Node manipulation results in invalid parent/child relationship.

    the structures are as follows:

    <person>
    <name/>
    <address/>
    <phone/>
    </person>

    and would like to copy this into the following format:

    <xformimport>
    <docID col="blah" id="blahblah"/>
    <document>
    <person>
    <name/>
    <address/>
    <phone/>
    </person>
    </document>
    </xformimport>

    I have written all the code to build 'xformimport','docID' and 'document',
    but just need to copy 'person' across.
    I figure i may be missing an easy solution here, so any help/ideas will be
    appreciated.
    I am using PyXML 0.8.1 and python 2.1.3.

    thanks
    Jeremy

    --
    Scott Yang :: scotty at yang.id.au :: http://scott.yang.id.au/ :: PGP:0xF9DCFA8C

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 24, '02 at 11:15a
activeSep 27, '02 at 12:29a
posts3
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase