I have done a lot of research on this topic, the issue I'm having differs depending on what method I'm using. The files used are XML files. what I'm trying to do is use a template file EX:
<?xml version= "1.0" encoding= "iso-8859-1"?>
<r:root xmlns:p="./file" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Version>1</Version>
<Parent Number="">
</Parent>
</r:root>
and insert a node from another file into the template under the parent tag. Insert file:
<?xml version= "1.0" encoding= "iso-8859-1"?>
<Child ID="" Type="">
<Sub1>text</Sub1>
<Sub2>text</Sub2>
<Sub3>text</Sub3>
<Sub4>text</Sub4>
<Nest1>
<Sub1>text</Sub1>
<Sub2>text</Sub2>
</Nest1>
</Child>
I'm currently trying to use the deepycopy method where I'm parsing the files and deepcopying the root.
lxml method issues: when I insert the node into the parent tree and try to print out the new tree this is the output.
<?xml version= "1.0" encoding= "iso-8859-1"?>
<r:root xmlns:p="./file" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Version>1</Version>
<Parent Number="">
<Child ID="" Type=""><Sub1>text</Sub1><Sub2>text</Sub2><Sub3>text</Sub3><Sub4>text</Sub4><Nest1><Sub1>text</Sub1><Sub2>text</Sub2></Nest1></Child></Parent>
</r:root>
elementtree method issues: I couldn't get the pretty print to look right use the minidom prettify, and would turn the r:root into ns0:root.
import xml.etree.ElementTree as ET
import xml.dom.minidom as MD
def prettify(root, encoder):
rough_string = ET.tostring(root, str(encoder))
reparse = MD.parseString(rough_string)
return reparse.topprettyxml(indent=" ", newl="")
beautifulsoup method issue: I got it to work when it was parsing with HTML but was lowercasing everything and I can't have that, wasn't able to get the xml parser to work.
all I need is for when I insert the node it keeps the pretty structure.
What am I doing wrong or missing here to make this work?