Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML walking in python [closed]

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?

Thanks

UPDATE:

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.

like image 303
Baywatch Avatar asked Nov 20 '12 02:11

Baywatch


People also ask

How do I open a XML file in Python?

To read an XML file using ElementTree, firstly, we import the ElementTree class found inside xml library, under the name ET (common convension). Then passed the filename of the xml file to the ElementTree. parse() method, to enable parsing of our xml file. Then got the root (parent tag) of our xml file using getroot().

How do I read an XML string in Python?

There are two ways to parse the file using 'ElementTree' module. The first is by using the parse() function and the second is fromstring() function. The parse () function parses XML document which is supplied as a file whereas, fromstring parses XML when supplied as a string i.e within triple quotes.

What is Etree in Python?

The xml.etree.ElementTree module implements a simple and efficient API for parsing and creating XML data. Changed in version 3.3: This module will use a fast implementation whenever available.

Which XML parser is best in Python?

If parsing speed is a key factor for you, consider using cElementTree or lxml.


1 Answers

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.

like image 171
aw4lly Avatar answered Oct 31 '22 16:10

aw4lly