8

I am new to python/lxml After reading the lxml site and dive into python I could not find the solution to my n00b troubles. I have the below xml sample:

---------------
<addressbook>
    <person>
        <name>Eric Idle</name>
        <phone type='fix'>999-999-999</phone>
        <phone type='mobile'>555-555-555</phone>
        <address>
            <street>12, spam road</street>
            <city>London</city>
            <zip>H4B 1X3</zip>
        </address>
    </person>
</addressbook>
-------------------------------

I am trying to append one child to the root element and write the entire file back out as a new xml or over write the existing xml. Currently all I am writing is one line.

from lxml import etree
tree = etree.parse('addressbook.xml')
root = tree.getroot()
oSetroot = etree.Element(root.tag)
NewSub = etree.SubElement ( oSetroot, 'CREATE_NEW_SUB' )
doc = etree.ElementTree (oSetroot)
doc.write ( 'addressbook1.xml' )

TIA

2
  • note that I edited your Q to format the code and XML legibly -- while writing a Q which contains code (or the like, like XML), highlight those blocks and click the icon that looks like a little square of 0s and 1s on the line of icons just above your text -- this is the simplest way to achieve the formatting (just indenting the whole block by four spaces and ensuring there are empty lines before and after is equivalent -- that's what clicking the icon does for you, btw;-). Commented Sep 6, 2010 at 2:37
  • @Alex, Thank you, so much to learn in so little time +1000 to you sir Commented Sep 6, 2010 at 4:46

1 Answer 1

17

You could make a new tree by copying over all of the old one (not just the root tag!-), but it's much simpler to edit the existing tree in-place (and, why not?-)...:

tree = etree.parse('addressbook.xml')
root = tree.getroot()
NewSub = etree.SubElement ( root, 'CREATE_NEW_SUB' )
tree.write ( 'addressbook1.xml' )

which puts in addressbook1.xml:

<addressbook>
    <person>
        <name>Eric Idle</name>
        <phone type="fix">999-999-999</phone>
        <phone type="mobile">555-555-555</phone>
        <address>
            <street>12, spam road</street>
            <city>London</city>
            <zip>H4B 1X3</zip>
        </address>
    </person>
<CREATE_NEW_SUB /></addressbook>

(which I hope is the effect you're looking for...?-)

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it is exactly! Bashing head on table Thank you!
@Nathaniel, you're welcome! Once you've tried the answer's suggestion out, if it works well and has thus helped you, remember to "accept" the answer (by clicking on the checkmark-shaped icon on the answer's left) -- that's core stack-overflow etiquette!-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.