Copying Elements using PyXML

Scott Yang scotty at yang.id.au
Thu Sep 26 20:29:09 EDT 2002


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



More information about the Python-list mailing list