Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The most efficient way to use SAX or similar parser. For instance Python with expat that gets distributed with Python:</p> <pre><code>#------------------------------------------------------------------------------- # Author: nad2000 AT google DOT com # # Saple demonstrating how to trasform: # # &lt;doc&gt; # &lt;a1&gt;11111&lt;/a1&gt; # &lt;b1&gt;22222&lt;/b1&gt; # &lt;c1&gt;33333&lt;/c1&gt; # &lt;/doc&gt; # # into: # # &lt;doc2&gt; # &lt;f1&gt;11111&lt;/f1&gt; # &lt;f2&gt;33333&lt;/f2&gt; # &lt;/doc2&gt; #------------------------------------------------------------------------------- #!/usr/bin/env python from xml.parsers import expat class DocParser: def __init__(self): self._parser = expat.ParserCreate() self._parser.StartElementHandler = self.start self._parser.EndElementHandler = self.end self._parser.CharacterDataHandler = self.data def feedFile(self, fileName): file= open( fileName, mode='rb') self._parser.ParseFile( file) file.close() def close(self): del self._parser # get rid of circular references def start(self, tag, attrs): if tag == 'doc': print ('&lt;?xml version="1.0" encoding="UTF-8"?&gt;') print ("&lt;doc2&gt;") def end(self, tag): if tag == 'a1': print ("\t&lt;f1&gt;%s&lt;/f1&gt;" % self._data) elif tag == 'c1': print ("\t&lt;f2&gt;%s&lt;/f2&gt;" % self._data) elif tag == 'doc': print ("&lt;/doc2&gt;") def data(self, data): self._data = data def main(): p = DocParser() p.feedFile( "sample.xml") p.close() pass if __name__ == '__main__': main() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload