Append xml to xml using python.I have two xml files which i need to merge .so is there anyway i can merge both files
Question: can anybody tell me how to append xml files using python

this is my file1.xml

    
    
    <?xml version="1.0"?>
    <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>
    

and i want to append it to another xml file

    
    
    <?xml version="1.0"?>
    
     <note>
        <to>Tove</to>
        <from>Jani</from>
        <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
     </note>
    

Is there any module which can do this for me...

and the output i needed is :

    
    
    <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>
        <note>
            <to>Tove</to>
            <from>Jani</from>
            <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
         </note>
    </addressbook>
    

And for now i need to read xml from files but later i need to get the xml
response from server and the convert it into one xml file .So please if any
one know that to it will help me alot......


Answer: **EDIT** : Removed old answer as the question changed considerably.

Using lxml:

    
    
    addressbook_xml = """<?xml version="1.0"?>
    <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>"""
    
    note_xml = """<?xml version="1.0"?>
    
     <note>
        <to>Tove</to>
        <from>Jani</from>
        <heading>Reminder</heading>
        <body>Don't forget me this weekend!</body>
     </note>
    """
    
    from lxml import etree
    
    # XML strings to etree
    addressbook_root = etree.fromstring(addressbook_xml)
    note_root = etree.fromstring(note_xml)
    
    # append the note
    addressbook_root.append(note_root)
    
    # print the new addressbook XML document
    print etree.tostring(addressbook_root)
    

