Solved

How do I insert an XML fragment into another XML tree using Python and lxml?

Posted on 2014-07-29
4
1,078 Views
Last Modified: 2014-07-29
I have a fragment of XML (the output from an XSL transform using lxml.etree), which I want to add to another XML tree.  I have
  <newsection>
    <newitem name="fred" attr1="7"/> 
    <newitem name="george" attr1="6"/>
  </newsection>

Open in new window

and
<oldparent>
  <oldsection>
    <oldchild>
      <oldgc name="sally"/>
    </oldchild>
  </oldsection>
  <othersect>
    <otherch name="alice"/>
  </othersect>
</oldparent>

Open in new window

Essentially, I need to insert the first fragment within the second so that "<newsection" is at the same level as "<oldsection>" and "<othersect>".  the location doesn't matter.

So far, I have tried inserting the first fragment entry by entry, but it ends up in the wrong order.  For example, using
#!/usr/bin/env python

import lxml.etree as ET

sd = ET.parse('file_containing_first_fragment.xml')
ft = ET.parse('extract_first_fragment.xsl')
transform = ET.XSLT(ft)
rx = transform(sd)
# "rx" now contains just the first fragment

st = ET.parse('second_fragment.xml')
root = st.getroot()
for e in rx.getiterator():
	root.append(e)
print(ET.tostring(st))

Open in new window

This does insert the first fragment, but the ordering is wrong - I get the "<newsection>" open and close before any of the "<newitem>" entries:
<oldparent>
  <oldsection>
    <oldchild>
      <oldgc name="sally"/>
    </oldchild>
  </oldsection>
  <othersect>
    <otherch name="alice"/>
  </othersect>
<newsection>
       </newsection>
<newitem name="fred" attr1="7"/> 
    <newitem name="george" attr1="6"/>
</oldparent>

Open in new window

How can I do this and have that "<newsection>" fragment in the right order?
0
Comment
Question by:simon3270
  • 2
  • 2
4 Comments
 
LVL 28

Accepted Solution

by:
pepr earned 500 total points
ID: 40226402
The following code is with the standard xml.etree.ElementTree, but it should be the same with the lxml:
#!python3

import xml.etree.ElementTree as ET

rx = ET.fromstring('''
<newsection>
  <newitem name="fred" attr1="7"/> 
  <newitem name="george" attr1="6"/>
</newsection>''')
# "rx" now contains just the first fragment

root = ET.fromstring('''
<oldparent>
  <oldsection>
    <oldchild>
      <oldgc name="sally"/>
    </oldchild>
  </oldsection>
  <othersect>
    <otherch name="alice"/>
  </othersect>
</oldparent>''')

root.append(rx)
ET.dump(root)

Open in new window

It prints
<oldparent>
  <oldsection>
    <oldchild>
      <oldgc name="sally" />
    </oldchild>
  </oldsection>
  <othersect>
    <otherch name="alice" />
  </othersect>
<newsection>
  <newitem attr1="7" name="fred" />
  <newitem attr1="6" name="george" />
</newsection></oldparent>

Open in new window

The root stores the element 'oldparent' as the list of its children. You want to append the rx element as another child (as a whole).
0
 
LVL 19

Author Comment

by:simon3270
ID: 40226537
Thanks, @pepr, you pointed me in the right direction.

Your example works fine with lxml.etree, but my code didn't - I got:
  File "./tstsd.py", line 13, in <module>
    root.append(rx)
AttributeError: 'lxml.etree._ElementTree' object has no attribute 'append'

My problem turned out to be that ET.parse (which I was using to read the XML file) returns an ElementTree, which doesn't have an "append" attribute. ET.fromstring (which you used) returns an Element, which does have one.

The fix was to make use the string attributes to make ElementTrees into Elements, so I did
    root = ET.parse('second_fragment.xml')
    root = ET.tostring(ET.fromstring(root))
Not pretty, but it worked!
0
 
LVL 28

Expert Comment

by:pepr
ID: 40226626
No, no! It is known that ET.parse() returns a tree object. The root element object is obtained from the tree object via calling its .getroot() method -- as you did in your code for example here:
...
st = ET.parse('second_fragment.xml')
root = st.getroot()

Open in new window

0
 
LVL 19

Author Comment

by:simon3270
ID: 40226960
Aha, even easier!  And certainly prettier. Many thanks.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python 2.7 - French characters 6 80
Parsing the XML data to SQL Server 4 65
Cassandra Select Query 1 75
configure dependency in POM for new database 3 25
Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question