Python-Ref > XML > SAX > Modifying data
 
 

<-^^
Klíčová slova
Moduly
Knihovní funkce

Modifying data

The xml.sax.saxutils module contains a ContentHandler implementation that outputs its data directly back to an opened XML file. This object can be conveniently used to make small adjustments to the XML document.
Expand/Shrink
<examples>
  <example>
    <title>
      Example 1 - introduction
    </title>
    <text>
      This is example nr. 1. It shows
      how an example looks.
    </text>
  </example>

  <example>
    <title>Example 2 - the    second part</title>
    <text>Another example.
    Imagine some ingenious text here...</text>
  </example>
</examples>
Zdroj: (sax3-0.py)
  1   import xml.sax
  2   from xml.sax.saxutils import XMLGenerator
  3   
  4   filename = "example4.xml"
  5   outfile = file( "output.xml", "w")
  6   writer = XMLGenerator( outfile)
  7   xml.sax.parse( filename, writer)
  8   outfile.close()
<?xml version="1.0" encoding="iso-8859-1"?>
<examples>
  <example>
    <title>
      Example 1 - introduction
    </title>
    <text>
      This is example nr. 1. It shows
      how an example looks.
    </text>
  </example>

  <example>
    <title>Example 2 - the    second part</title>
    <text>Another example.
    Imagine some ingenious text here...</text>
  </example>
</examples>
Doba běhu: 76.6 ms
Expand/Shrink
Zdroj: (sax3-3.py)
  1   import xml.sax
  2   from xml.sax.saxutils import XMLGenerator
  3   
  4   outfile = file( "output.xml", "w")
  5   writer = XMLGenerator( outfile)
  6   writer.startElement( "output", {})
  7   # example 1
  8   writer.startElement( "example", {"id":"1"})
  9   writer.characters( "This is example 1.")
 10   writer.endElement( "example")
 11   # example 2
 12   writer.startElement( "example", {"id":"2"})
 13   writer.characters( "This is example 2.")
 14   writer.endElement( "example")
 15   writer.endElement( "output")
 16   outfile.close()
<output><example id="1">This is example 1.</example><example id="2">This is example 2.</example></output>
Doba běhu: 74.9 ms
Expand/Shrink
<examples>
  <example>
    <title>
      Example 1 - introduction
    </title>
    <text>
      This is example nr. 1. It shows
      how an example looks.
    </text>
  </example>

  <example>
    <title>Example 2 - the    second part</title>
    <text>Another example.
    Imagine some ingenious text here...</text>
  </example>
</examples>
Zdroj: (sax3-1.py)
  1   import xml.sax
  2   from xml.sax.saxutils import XMLGenerator
  3   
  4   class MyHandler ( xml.sax.ContentHandler):
  5   
  6     def __init__( self, writer):
  7       xml.sax.ContentHandler.__init__( self)
  8       self.writer = writer
  9       self._example_count = 0
 10   
 11     def startElement( self, name, attrs):
 12       if name == "example":
 13         self._example_count += 1
 14         if "id" not in attrs.keys():
 15           attrs = dict( attrs.items())
 16           attrs['id'] = "e%02d" % self._example_count
 17       self.writer.startElement( name, attrs)
 18   
 19     def endElement( self, name):
 20       self.writer.endElement( name)
 21   
 22     def characters( self, data):
 23       if not data.isspace():
 24         data = " ".join( data.split()) # whitespace normalization
 25       self.writer.characters( data)
 26   
 27   
 28   filename = "example4.xml"
 29   outfile = file( "processed.xml", "w")
 30   writer = XMLGenerator( outfile)
 31   handler = MyHandler( writer)
 32   xml.sax.parse( filename, handler)
 33   outfile.close()
<examples>
  <example id="e01">
    <title>
Example 1 - introduction
    </title>
    <text>
This is example nr. 1. It shows
how an example looks.
    </text>
  </example>

  <example id="e02">
    <title>Example 2 - the second part</title>
    <text>Another example.
Imagine some ingenious text here...</text>
  </example>
</examples>
Doba běhu: 76.8 ms
Expand/Shrink
<examples>
  <example>
    <title>
      Example 1 - introduction
    </title>
    <text>
      This is example nr. 1. It shows
      how an example looks.
    </text>
  </example>

  <example>
    <title>Example 2 - the    second part</title>
    <text>Another example.
    Imagine some ingenious text here...</text>
  </example>
</examples>
Zdroj: (sax3-2.py)
  1   import xml.sax
  2   from xml.sax.saxutils import XMLGenerator
  3   
  4   class MyHandler ( xml.sax.ContentHandler):
  5   
  6     def __init__( self, writer):
  7       xml.sax.ContentHandler.__init__( self)
  8       self.writer = writer
  9       self._example_count = 0
 10       self._characters = ""
 11   
 12     def startElement( self, name, attrs):
 13       self._write_characters()
 14       if name == "example":
 15         self._example_count += 1
 16         if "id" not in attrs.keys():
 17           attrs = dict( attrs.items())
 18           attrs['id'] = "e%02d" % self._example_count
 19       self.writer.startElement( name, attrs)
 20   
 21     def endElement( self, name):
 22       self._write_characters()
 23       self.writer.endElement( name)
 24   
 25     def characters( self, data):
 26       self._characters += data
 27   
 28     def _write_characters( self):
 29       data = self._characters
 30       if not data.isspace():
 31         data = " ".join( data.split()) # whitespace normalization
 32       self.writer.characters( data)
 33       self._characters = ""
 34   
 35   
 36   
 37   filename = "example4.xml"
 38   outfile = file( "processed.xml", "w")
 39   writer = XMLGenerator( outfile)
 40   handler = MyHandler( writer)
 41   xml.sax.parse( filename, handler)
 42   outfile.close()
<examples>
  <example id="e01">
    <title>Example 1 - introduction</title>
    <text>This is example nr. 1. It shows how an example looks.</text>
  </example>

  <example id="e02">
    <title>Example 2 - the second part</title>
    <text>Another example. Imagine some ingenious text here...</text>
  </example>
</examples>
Doba běhu: 77.0 ms