0

I have one xml file that looks like this, XML1:

<?xml version='1.0' encoding='utf-8'?>
<report>
</report>

And the other one that is like this, XML2:

<?xml version='1.0' encoding='utf-8'?>
<report attrib1="blabla" attrib2="blabla" attrib3="blabla" attrib4="blabla" attrib5="blabla" >
    <child1>  
        <child2> 
            ....
        </child2>
    </child1>
</report>

I need to replace and put root element of XML2 without its children, so XML1 looks like this:

<?xml version='1.0' encoding='utf-8'?>
<report attrib1="blabla" attrib2="blabla" attrib3="blabla" attrib4="blabla" attrib5="blabla">
</report>

Currently my code looks like this but it won't remove children but put whole tree inside:

source_tree = ET.parse('XML2.xml')
source_root = source_tree.getroot()

report = source_root.findall('report') 

for child in list(report):
     report.remove(child)
     source_tree.write('XML1.xml', encoding='utf-8', xml_declaration=True)

Anyone has ide how can I achieve this?

Thanks!

4
  • Does this answer your question? XML: remove child node of a node Commented Oct 7, 2021 at 8:29
  • Unfortunately noup, because here he knows what are tags of children, but in my case they are changing, so this second iteration bars = foo.findall('bar') won't work in my situation Commented Oct 7, 2021 at 8:36
  • You just need to copy the attrib from 2 to 1. See my answer. Commented Oct 7, 2021 at 8:38
  • recursively iterate over xml: stackoverflow.com/questions/21074361/… Commented Oct 7, 2021 at 8:38

2 Answers 2

1

Try the below (just copy attrib)

import xml.etree.ElementTree as ET


xml1 = '''<?xml version='1.0' encoding='utf-8'?>
<report>
</report>'''

xml2 = '''<?xml version='1.0' encoding='utf-8'?>
<report attrib1="blabla" attrib2="blabla" attrib3="blabla" attrib4="blabla" attrib5="blabla" >
    <child1>  
        <child2> 
        </child2>
    </child1>
</report>'''

root1 = ET.fromstring(xml1)
root2 = ET.fromstring(xml2)

root1.attrib = root2.attrib

ET.dump(root1)

output

<report attrib1="blabla" attrib2="blabla" attrib3="blabla" attrib4="blabla" attrib5="blabla">
</report>
Sign up to request clarification or add additional context in comments.

7 Comments

It's a such a fine solution, but I keep getting: ParseError not well-formed (invalid token): line 1, column 2
If you copy & paste my solution - do you get any error? Please explain what you do.
I just put root1 = ET.fromstring(xml1) and replace xml1 with whole path to the file like this ('C:/myPc/blabla/xml1.xml') and get error on that line
now I have managed to get it running, but attribute value is only printed in my console, not actually copied in file
@John, to save edited XML to file you need .... to save XML to file. root.write()
|
0

So here is working code:

source_tree = ET.parse('XML2.xml')
source_root = source_tree.getroot()

dest_tree = ET.parse('XML1.xml')
dest_root = dest_tree.getroot()

dest_root.attrib = source_root.attrib
dest_tree.write('XML1.xml', encoding='utf-8', xml_declaration=True)

Comments

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.