Simple XML Document Analysis : XML Parse « XML « Python






Simple XML Document Analysis

 

import sys
from xml.parsers import expat

def start_element(name,attrs):
    print 'Start:', name, attrs

def character_data(data):
    print 'Data:', repr(data)

def end_element(name):
    print 'End: ', name

p = expat.ParserCreate()

p.StartElementHandler = start_element
p.EndElementHandler = end_element
p.CharacterDataHandler = character_data

p.ParseFile(open(sys.argv[1]))

   
  








Related examples in the same category

1.Extracting the Ingredients from a Recipe with Regular Expressions
2.Processing XML with Regular Expressions
3.Parsing XML from Local Files and Remote URLsParsing XML from Local Files and Remote URLs
4.Parsing XML from a URL
5.Parsing XML from StringsParsing XML from Strings
6.Parsing XML: Loading an XML Document