Walk through all the XML nodes in an elementary structure

I have such an XML structure (output from Esprima ASL converted from JSON), it can become even more nested than this ( ASL.xml ):

 <?xml version="1.0" encoding="UTF-8" ?> <program> <type>Program</type> <body> <type>VariableDeclaration</type> <declarations> <type>VariableDeclarator</type> <id> <type>Identifier</type> <name>answer</name> </id> <init> <type>BinaryExpression</type> <operator>*</operator> <left> <type>Literal</type> <value>6</value> </left> <right> <type>Literal</type> <value>7</value> </right> </init> </declarations> <kind>var</kind> </body> </program> 

I usually use for node in root.childNodes` for XML, but this only works for direct children:

 import xml.dom.minidom as md dom = md.parse("ASL.xml") root = dom.documentElement for node in root.childNodes: if node.nodeType == node.ELEMENT_NODE: print node.tagName,"has value:", node.nodeValue:, "and is child of:",node.parentNode.tagName 

How can I move all XML elements no matter how many nested elements?

+4
source share
3 answers

This is probably best achieved with a recursive function. Something like this should do it, but I have not tested it, so consider its pseudo-code.

 import xml.dom.minidom as md def print_node(root): if root.childNodes: for node in root.childNodes: if node.nodeType == node.ELEMENT_NODE: print node.tagName,"has value:", node.nodeValue, "and is child of:", node.parentNode.tagName print_node(node) dom = md.parse("ASL.xml") root = dom.documentElement print_node(root) 
+9
source

If it is not important to use xml.dom.minidom:

 import xml.etree.ElementTree as ET tree = ET.fromstring("""...""") for elt in tree.iter(): print "%s: '%s'" % (elt.tag, elt.text.strip()) 

Output:

 program: '' type: 'Program' body: '' type: 'VariableDeclaration' declarations: '' type: 'VariableDeclarator' id: '' type: 'Identifier' name: 'answer' init: '' type: 'BinaryExpression' operator: '*' left: '' type: 'Literal' value: '6' right: '' type: 'Literal' value: '7' kind: 'var' 
+2
source

For the equivalent of 2.6+ kalgasnik Elementree code, simply replace iter () with getiterator ():

 import xml.etree.ElementTree as ET tree = ET.fromstring("""...""") for elt in tree.getiterator(): print "%s: '%s'" % (elt.tag, elt.text.strip()) 
+1
source

Source: https://habr.com/ru/post/1437147/


All Articles