Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minidom: How can I check if I have the expected root and children?

I have this xml structure,

<root>

    <child1>
    </child1>
    <child2>
    </child2>
    <child3 />
    <extendedchild:name>
    </extendedchild:name>

</root>

How can I check with minidom, that root is root, and that children are always the followin elements?

child1
child2
child3
extendedchild

I want also to print the elements that are out of root or not in the above "children list"(outofroot, notachild): ...

    <notachild />
</root>
<outofroot />

Edit: It seems that outofroot element is treated by the minidom parser, it gives xxxxxxx.xml has an error: junk after document element: line 12, column 0

like image 328
Eduard Florinescu Avatar asked Dec 27 '25 16:12

Eduard Florinescu


2 Answers

You can use minidom to walk though the children and verify that the name of the root node is "root". You can then process children one at a time and verify the other requirements.

if not root.tagName == "root":
   # do something
for node in root.childNodes:
   # do something more

If necessary, you can process the child nodes recursively.

def processChild(node):
    # do some checks on node
    for child in node.childNodes:
        processChild(child)
like image 190
Hans Then Avatar answered Dec 30 '25 05:12

Hans Then


As far as the root element name check, looks like you can do it like this:

import xml.dom.minidom
dom = xml.dom.minidom.parseString(xmlString)
if dom.documentElement.tagName == "root" ...

You should be able to for..in iterate on the root's .childNodes.

If something is outside of the root, it is not a well-formed XML document (which can only have one root node).

like image 41
Brett Zamir Avatar answered Dec 30 '25 05:12

Brett Zamir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!