Python xml hosting

I am new to python and would like to understand the xml parsing. I could not find any wonderful examples or explanations of how to create a general program for working with a node of XML nodes.

I want to be able to classify and identify all elements and attributes by name and value, without having any xml schema information. I do not want to rely on invoking elements and attributes specifically by name or tag.

Can someone point me in the right direction?

thanks

UPDATE:

The specific question that was asked was that "as usual, I recurse all the nodes from the root of the node in the XML document without any deep knowledge of the schema."

At that time, being new to python and understanding how to perform this operation in many other languages, I was puzzled by any real world examples that did not rely on named nodes to traverse the DOM, which is not what I wanted at all.

We hope that this clarifies the issue, since the information in this thread is really useful.

+4
source share
2 answers

Check out the ElementTree documentation in python help

Base code stub from this page:

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

you can continue to work for child in root: recursively down until there are no more children.

+4
source

use cElementTree; it is 15-20 times faster than the ElementTree version in Python, and uses memory 2-5 times less. http://effbot.org/zone/celementtree.htm

 import xml.etree.cElementTree as ET tree = ET.parse('test.xml') for elem in tree.getiterator(): if elem.tag: print 'my name:' print '\t'+elem.tag if elem.text: print 'my text:' print '\t'+(elem.text).strip() if elem.attrib.items(): print 'my attributes:' for key, value in elem.attrib.items(): print '\t'+'\t'+key +' : '+value if list(elem): # use elem.getchildren() for python2.6 or before print 'my no of child: %d'%len(list(elem)) else: print 'No child' if elem.tail: print 'my tail:' print '\t'+'%s'%elem.tail.strip() print '$$$$$$$$$$' 
+3
source

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


All Articles