RPGLE XML Parser – XML-SAX as well as XML-INTO sample code

What is XML as well as why practice nosotros involve XML inward today ?
Well the really argue y’all are looking this post today is y’all came across an XML information from a 3rd political party vendor that wants to communicate amongst y’all as well as y’all involve the information to parse it as well as set it into the relational database close in all probability a Db2 table. Now I lead keep been parsing XML information for a acre using Java Xerces parser from Apache. I took a peek at the RPG parser a few years dorsum but it didn’t accommodate me every bit at that spot is likewise much coding involved. Well the really sentiment I similar RPG over COBOL is the same argue why I liked Java parser over the RPG parser.

But straightaway y’all don’t fifty-fifty lead keep to acquire Op-Codes to practice parsing  when using XML-SAX. I lead keep given hither an actual code that volition assist y’all parse as well as explicate how to practice it without the cognition of parsing an XML. Before I acquire ahead amongst lawsuit code for RPGLE lets reckon how the inner mechanics of XML-SAX work.
— OR —
Click hither to skip this as well as saltation straight to lawsuit code using XML-SAX.

The DOM parser available inward RPG using op-code XML-INTO tin john practice footling materials but its annoying for me to define all these information structures. Plus the DOM parser XML-INTO op-code has its limitations too. They are good documented yesteryear Scott Klement inward his articles. But lately I came across a asking to practice parsing for a projection that involved a lot of these XML information as well as amongst science limitation of personnel  knowing Java along amongst iSeries Back-End forced me accept a difficult hold back i time to a greater extent than at the RPG parser. Here is when I came across about other document yesteryear Scott Klement why he likes the SAX parser XML-SAX over the DOM parser XML-INTO.


While RPG’s XML-INTO op-code makes XML parsing easy, it has its limitations. It doesn’t piece of job when at that spot are particular characters inward a tag refer or when the refer of the XML chemical ingredient is stored inward a variable. Or if you’re nonetheless running 5.4, it can’t handgrip grapheme information as well as attributes inward the same element. When y’all striking these limits, it’s XML-SAX to the rescue! Also inward improver to that it cumbersome to define the Data Structures amongst same layout as well as champaign names every bit the XML document.

Benefits of SAX Parser

  • SAX parsers lead keep for certain benefits over DOM-style parsers. The quantity of retention that a SAX parser must usage inward social club to constituent is typically much smaller than that of a DOM parser. DOM parsers must lead keep the entire tree inward retention earlier whatsoever processing tin john begin, as well as then the amount of retention used yesteryear a DOM parser depends alone on the size of the input data. The retention footprint of a SAX parser, yesteryear contrast, is based simply on the maximum depth of the XML file (the maximum depth of the XML tree) as well as the maximum information stored inward XML attributes on a unmarried XML element. Both of these are e’er smaller than the size of the parsed tree itself.
  • Because of the event-driven nature of SAX, processing documents tin john oftentimes live on faster than DOM-style parsers. Memory allotment takes time, as well as then the larger retention footprint of the DOM is also a performance issue.
  • Due to the nature of DOM, streamed reading from disk is impossible. Processing XML documents larger than principal retention is also impossible amongst DOM parsers but tin john live on done amongst SAX parsers. However, DOM parsers may brand usage of disk infinite every bit retention to sidestep this limitation.
  • Say y’all lead keep to procedure i social club per XML document DOM is skilful plenty but for a whole batch of Orders coming inward an XML document I would rather acquire amongst XML-SAX parser.

SAX (Simple API for XML) is an event-based sequential access parser API developed yesteryear the XML-DEV mailing listing for XML documents. Influenza A virus subtype H5N1 parser that implements SAX (i.e., a SAX Parser) functions every bit a current parser, amongst an event-driven API. The user defines a number of callback methods that volition live on called when events occur during parsing. The SAX events include:

  • XML Text nodes
  • XML Element nodes
  • XML Processing Instructions
  • XML Comments

Events are fired when each of these XML features are encountered, as well as i time to a greater extent than when the cease of them is encountered. XML attributes are provided every bit constituent of the information passed to chemical ingredient events.SAX parsing is unidirectional; previously parsed information cannot live on re-read without starting the parsing functioning again.

The syntax of the XML-SAX op-code itself is unproblematic enough. It volition hold back something similar this if you’re parsing the XML from a variable inward your program:

xml-sax %handler( your-handler-here : your-parameter-here )
%XML( xml string : ‘doc=string’);

Or it volition hold back similar this if you’re parsing an XML document that’s stored inward a current file inward the IFS:

xml-sax %handler( your-handler-here : your-parameter-here )
%XML( ifs path refer : ‘doc=file’);

Click hither for adjacent Chapter

Recommended Reading

Sumber http://www.mysamplecode.com/