Baywatch Baywatch - 6 months ago 9
Python Question

XML walking in python

I am new to python and would like to understand parsing xml. I have not been able to find any great examples or explanations of how to create a generic program to walk an XML nodeset.

I want to be able to categorize and identify all elements and attributes by name and value, without having any information about the xml schema. I don't want to rely on calling elements and attributes specifically by tag name or text.

Could someone please point me in the right direction?



The specific question that was being asked was, "how do I generally recurse all nodes from the root node in an XML document without having any intimate knowledge about the schema."

At the time, being new to python and understanding how to perform that operation in many other languages, I was perplexed by any real world examples that didn't rely on named nodes to traverse the DOM, which isn't what I wanted at all.

Hope this clarifies the question, as the information in this thread is indeed useful.


Check out the documentation of ElementTree on the python help

A basic stub of code from that page is:

    import xml.etree.ElementTree as ET
    tree = ET.parse(filename)
    root = tree.getroot()
    for child in root:  
      child.tag, child.attrib

you can keep running for child in root: recursively downward until there aren't any more children.