Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2014-07-29
4
1,155 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 29

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 29

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Article by: Swadhin
Introduction of Lists in Python: There are six built-in types of sequences. Lists and tuples are the most common one. In this article we will see how to use Lists in python and how we can utilize it while doing our own program. In general we can al…
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…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

828 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