I am new to xml and was trying to copy a node. Though it copies the node, when i append it, the closing tags mismatch. Here is the xml that i am parsing.
<doc>
<branch name="release01" hash="f200013e">
<sub-branch name="subrelease01">
xml,sgml
</sub-branch>
</branch>
</doc>
Here is the code that i am using to parse the xml:
import lxml.etree as ET
import copy
tree = ET.ElementTree(file="doc2.xml")
root = tree.getroot()
lst_nodes = tree.findall("branch")
ele = 0
while ele < len(lst_nodes):
ref = lst_nodes[ele]
if (lst_nodes[ele].attrib.get("name") == "release01"):
count = 0
while count < 1:
copied = copy.deepcopy(ref)
ref.append(copied)
count=count+1
ele+=1
ET.dump(root)
The output observed is:
<doc>
<branch name="release01" hash="f200013e">
<sub-branch name="subrelease01">
xml,sgml
</sub-branch>
<branch name="release01" hash="f200013e">
<sub-branch name="subrelease01">
xml,sgml
</sub-branch>
</branch>
</branch>
</doc>
As you can see end tag of "branch" is mismatched. Can someone help me to identify the mistake that i am doing while copying or appending the node?