Python-Ref > XML > SAX > Reading data using SAX
 
 

<-^^->
Moduly
Knihovní funkce

Reading data using SAX

Expand/Shrink
<people>
  <person id="1">
    <first_name>Arthur</first_name>
    <family_name>Dent</family_name>
  </person>
  <person id="2">
    <first_name>Ford</first_name>
    <family_name>Prefect</family_name>
  </person>
  <person id="3">
    <first_name>Zaphod</first_name>
    <family_name>Beeblebrox</family_name>
  </person>
  <person id="4">
    <first_name>Tricia</first_name>
    <family_name>McMillan</family_name>
  </person>
  <person id="5">
    <first_name>Marvin</first_name>
    <family_name>Paranoid Android</family_name>
  </person>
</people>
Zdroj: (sax2-1.py)
  1   import xml.sax
  2   
  3   class MyHandler ( xml.sax.ContentHandler):
  4   
  5     def __init__( self):
  6       xml.sax.ContentHandler.__init__( self)
  7       self._above = []
  8       self._person = None
  9       self.persons = []
 10   
 11     def startElement( self, name, attrs):
 12       if name == "person":
 13         self._person = {}
 14         if "id" in attrs.keys():
 15           self._person['id'] = attrs['id']
 16       self._above.append( name)
 17   
 18     def endElement( self, name):
 19       if name == "person":
 20         self.persons.append( self._person)
 21         self._person = None
 22       self._above.pop( -1)
 23   
 24     def characters( self, data):
 25       if self._above[-1] == "first_name":
 26         self._person['first_name'] = self._person.get( "first_name", "") + data
 27       elif self._above[-1] == "family_name":
 28         self._person['family_name'] = self._person.get( "family_name", "") + data
 29   
 30   filename = "example3.xml"
 31   handler = MyHandler()
 32   xml.sax.parse( filename, handler)
 33   for person in handler.persons:
 34     print "%(first_name)s %(family_name)s [id=%(id)s]" % person
stdout:
Arthur Dent [id=1]
Ford Prefect [id=2]
Zaphod Beeblebrox [id=3]
Tricia McMillan [id=4]
Marvin Paranoid Android [id=5]
Doba běhu: 87.5 ms
Expand/Shrink
<people>
  <person id="1">
    <first_name>Arthur</first_name>
    <family_name>Dent</family_name>
  </person>
  <person id="2">
    <first_name>Ford</first_name>
    <family_name>Prefect</family_name>
  </person>
  <person id="3">
    <first_name>Zaphod</first_name>
    <family_name>Beeblebrox</family_name>
  </person>
  <person id="4">
    <first_name>Tricia</first_name>
    <family_name>McMillan</family_name>
  </person>
  <person id="5">
    <first_name>Marvin</first_name>
    <family_name>Paranoid Android</family_name>
  </person>
</people>
Zdroj: (sax2-2.py)
  1   import xml.sax
  2   
  3   class Person( object):
  4   
  5     def __init__( self, first_name="", family_name="", id=0):
  6       self.first_name = first_name
  7       self.family_name = family_name
  8       self.id = id
  9   
 10     def __str__( self):
 11       return "%s %s [id=%s]" % (self.first_name, self.family_name, self.id)
 12   
 13   
 14   class MyHandler ( xml.sax.ContentHandler):
 15   
 16     def __init__( self):
 17       xml.sax.ContentHandler.__init__( self)
 18       self._above = []
 19       self._person = None
 20       self.persons = []
 21   
 22     def startElement( self, name, attrs):
 23       if name == "person":
 24         self._person = Person()
 25         if "id" in attrs.keys():
 26           self._person.id = attrs['id']
 27       self._above.append( name)
 28   
 29     def endElement( self, name):
 30       if name == "person":
 31         self.persons.append( self._person)
 32         self._person = None
 33       self._above.pop( -1)
 34   
 35     def characters( self, data):
 36       if self._above[-1] == "first_name":
 37         self._person.first_name += data
 38       elif self._above[-1] == "family_name":
 39         self._person.family_name += data
 40   
 41   filename = "example3.xml"
 42   handler = MyHandler()
 43   xml.sax.parse( filename, handler)
 44   for person in handler.persons:
 45     print person
stdout:
Arthur Dent [id=1]
Ford Prefect [id=2]
Zaphod Beeblebrox [id=3]
Tricia McMillan [id=4]
Marvin Paranoid Android [id=5]
Doba běhu: 90.4 ms