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
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)
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With