home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / python-libxml2 / examples / reader5.py < prev    next >
Encoding:
Python Source  |  2004-02-23  |  1.2 KB  |  49 lines

  1. #!/usr/bin/python -u
  2. #
  3. # this tests the Expand() API of the xmlTextReader interface
  4. # this extract the Dragon bibliography entries from the XML specification
  5. #
  6. import libxml2
  7. import StringIO
  8. import sys
  9.  
  10. # Memory debug specific
  11. libxml2.debugMemory(1)
  12.  
  13. expect="""<bibl id="Aho" key="Aho/Ullman">Aho, Alfred V., 
  14. Ravi Sethi, and Jeffrey D. Ullman.
  15. <emph>Compilers:  Principles, Techniques, and Tools</emph>.
  16. Reading:  Addison-Wesley, 1986, rpt. corr. 1988.</bibl>"""
  17.  
  18. f = open('../../test/valid/REC-xml-19980210.xml')
  19. input = libxml2.inputBuffer(f)
  20. reader = input.newTextReader("REC")
  21. res=""
  22. while reader.Read():
  23.     while reader.Name() == 'bibl':
  24.         node = reader.Expand()            # expand the subtree
  25.         if node.xpathEval("@id = 'Aho'"): # use XPath on it
  26.             res = res + node.serialize()
  27.         if reader.Next() != 1:            # skip the subtree
  28.             break;
  29.  
  30. if res != expect:
  31.     print "Error: didn't get the expected output"
  32.     print "got '%s'" % (res)
  33.     print "expected '%s'" % (expect)
  34.     
  35.  
  36. #
  37. # cleanup
  38. #
  39. del input
  40. del reader
  41.  
  42. # Memory debug specific
  43. libxml2.cleanupParser()
  44. if libxml2.debugMemory(1) == 0:
  45.     print "OK"
  46. else:
  47.     print "Memory leak %d bytes" % (libxml2.debugMemory(1))
  48.     libxml2.dumpMemory()
  49.