<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>I never use XML::SAX in practice. It’s too much work. </div><div><br></div><div>I usually use XML::LibXML::Reader, which allows more or less streaming access to elements. I can read in elements, even from STDIN, and then for a given element, I can turn it into a DOM tree and use XPath to get the bits I need. XML::LibXML is C underneath, so this is fast. It also handles files that are too big to fit in memory, which was how I ended up with this toolchain. It looks a bit like this:</div><div><br></div><div>my $reader = XML::LibXML::Reader->new(IO => $fh);</div><div><div>while($reader->read() && $reader->name() ne 'mutation') {};</div><div>    do {</div><div>        if ($reader->name() eq 'mutation') {</div><div>            my $root = $reader->copyCurrentNode(1);</div><div>            my $name = $root->findvalue(qq{/Entrezgene-Set/Entrezgene/Entrezgene_gene/Gene-ref/Gene-ref_locus});</div><div>            … and similar extractions</div><div>        }</div><div>    } while($reader->nextSibling());</div></div><div><br></div><div>This does assume that my file contains lots of sibling mutation elements, but nothing about what each contains.</div><div><br></div><div>The advantage of this is that actually using SAX to get at /mutation/Entrezgene-Set/Entrezgene/Entrezgene_gene/Gene-ref/Gene-ref_locus nested elements would be extremely painful. </div><div><br></div><div>Oh, and $fh can easily be \*STDIN if you need. </div><div><br></div><div>Seriously, XML::LibXML is a great set of tools for XML. Using SAX directly is going to be miserable. Also, XML::LibXML has decent API documentation, although the examples might leave something to be desired. </div><div><br></div><div>All the best</div><div>Stuart</div><div><br></div><div><br></div><br><div><div>On Nov 8, 2013, at 9:30 AM, <a href="mailto:arocker@Vex.Net">arocker@Vex.Net</a> wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><br>Does anyone know of a good explanation/example of how to use XML::SAX<br>anywhere online?<br><br>I've looked at the CPAN documentation, and other usual sources, but they<br>have some ambiguity about what are standard method names, features, &c.,<br>and what are simply examples of user-written resources.<br><br>The problem that started this train of thought is absurdly simple;<br>extracting 3 fields per record from a STDIN stream of records containing 5<br>or 6 embedded in XML tags.<br><br> A simple program, basically 3 regexes and a print, does the job, but<br>attracted a storm of online criticism about parsing XML with regexes.<br>I've been trying to write the equivalent using XML::SAX, but seem to be<br>missing something. (For one thing, all the examples assume reading from a<br>named file, not STDIN.)<br><br>_______________________________________________<br>toronto-pm mailing list<br><a href="mailto:toronto-pm@pm.org">toronto-pm@pm.org</a><br>http://mail.pm.org/mailman/listinfo/toronto-pm<br></blockquote></div><br><div apple-content-edited="true">
<span class="Apple-style-span" style="border-collapse: separate; border-spacing: 0px;"><span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-style: normal; font-variant: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;  "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><p style="font-weight: normal; font-family: Arial, sans-serif; font-size: 8pt; margin-bottom: 0px; ">--</p><div style="font-weight: normal; font-family: Verdana, sans-serif; font-size: 10pt; color: rgb(57, 121, 59); margin-top: 0px; margin-bottom: 0px; "><b>Stuart Watt</b></div><p style="font-weight: normal; font-family: Verdana, sans-serif; font-size: 8pt; margin-top: 0px; "><a href="mailto:stuart@morungos.com">stuart@morungos.com</a> / <span class="Apple-style-span" style="font-family: Helvetica;  "><a href="http://twitter.com/morungos">twitter.com/morungos</a></span></p></div></span></span>
</div>
<br></body></html>