Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Using ElementTree

ElementTree is a library that make XML processing in python
much, much easier. It will be included in Python 2.5 too.
Here's how to use it to read/extract XML
   1  
   2  from elementtree.ElementTree import parse
   3  tree = parse(filename)
   4  doc = tree.getroot()
   5  
   6  # Element type (name): 
   7  print doc.tag
   8  # Element text: 
   9  print doc.text
  10  # get the child of element type book
  11  book = doc.find('book')
  12  
  13  # element's attribute
  14  book.keys()
  15  book.items()
  16  book.get('COLOR')
  17  
  18  # first matching element's text
  19  booktext = doc.findtext('book')

Read more here and here.

Efficiently process big xml files with cElementTree

ElementTree is becoming python's standard XML framework.
It also supports processing data as it coming/loading.
   1  
   2  import cElementTree
   3  
   4  for event, elem in cElementTree.iterparse(file):
   5      if elem.tag == "record":
   6          ... process record element ...
   7          elem.clear()
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS