Skip to content Skip to sidebar Skip to footer

How Does One Reorder Information In An Xml Document In Python 3?

Let's suppose I have the following XML structure: <

Solution 1:

I'm not going to give you the code you want. Instead I'll say how you can go about doing what you want.

First things first you want to read your xml. So I'll be using xml.etree.ElementTree.

import xml.etree.ElementTree as ETroot= ET.fromstring(country_data_as_string)

After this I'd ignore the parts of the tree that you don't use, and just findCstmrCdtTrfInitn. As you only want to work with PmtInfs you want to findall of them.

pmt_infs = root.find('.//CstmrCdtTrfInitn').findall('PmtInf')

After this you want to perform your algorithm to move items on your data. I'll just remove the first child, if the node has one.

nodes= []
for node in pmt_infs:children=list(node)if children:node.remove(children[0])nodes.append(children[0])

Now that we have all the nodes, you'll add them to the first pmt_infs.

pmt_infs[0].extend(nodes)

You'll want to change the third code block to how you want to move your nodes, as you changed your algorithm from v1 to v3 of your question.

Post a Comment for "How Does One Reorder Information In An Xml Document In Python 3?"