RPGLE XML Parser – XML-SAX in addition to XML-INTO sample code Part 2

Click hither to read from initiative of all Chapter

XML-SAX Events

During the SAX parsing of your XML document, several XML events volition travel passed to your XML-SAX treatment procedure. To position the events inside your procedure, purpose the exceptional names starting amongst *XML, for instance *XML_START_ELEMENT. For nearly events, the treatment physical care for volition travel passed a value associated amongst the event. For example, for the *XML_START_ELEMENT event, the value is the get upwards of the XML element.

Event Value
1. Events discovered earlier the initiative of all XML chemical component
*XML_START_DOCUMENT Indicates that parsing has begun
*XML_VERSION_INFO The “version” value from the XML announcement
*XML_ENCODING_DECL The “encoding” value from the XML announcement
*XML_STANDALONE_DECL The “standalone” value from the XML announcement
*XML_DOCTYPE_DECL The value of the Document Type Declaration
2. Events related to XML elements
*XML_START_ELEMENT The get upwards of the XML chemical component that is starting
*XML_CHARS The value of the XML chemical component
*XML_PREDEF_REF The value of a predefined reference
*XML_UCS2_REF The value of a UCS-2 reference
*XML_UNKNOWN_REF The get upwards of an unknown entity reference
*XML_END_ELEMENT The get upwards of the XML chemical component that is ending
3. Events related to XML attributes
*XML_ATTR_NAME The get upwards of the attribute
*XML_ATTR_CHARS The value of the attribute
*XML_ATTR_PREDEF_REF The value of a predefined reference
*XML_ATTR_UCS2_REF The value of a UCS-2 reference
*XML_UNKNOWN_ATTR_REF The get upwards of an unknown entity reference
*XML_END_ATTR Indicates the destination of the attribute
4. Events related to XML processing instructions
*XML_PI_TARGET The get upwards of the target
*XML_PI_DATA The value of the information
5. Events related to XML CDATA sections
*XML_START_CDATA The commencement of the CDATA department
*XML_CHARS The value of the CDATA department
*XML_END_CDATA The destination of the CDATA department
6. Other events
*XML_COMMENT The value of the XML comment
*XML_EXCEPTION Indicates that the parser discovered an mistake
*XML_END_DOCUMENT Indicates that parsing has ended

Read IBM document for to a greater extent than details

Demonstration of Events as well as Data

The initiative of all fourth dimension you lot run across it, it tin give notice travel difficult to empathise the menses of events created past times XML-SAX. Therefore purpose the next plan that prints out each termination every bit it occurs. You tin give notice footstep through this plan inwards debug as well as run across which events occur at which times. Or you lot tin give notice only await at the printout it creates, which lists the events that occurred.

  H DFTACTGRP(*NO)      FQSYSPRT   O    F  132        PRINTER       D PrintMe         ds                  qualified      D   get upwards                        21a      D   information                       111a       D XML_Event_Name  PR            20A   varying      D    termination                      10i 0 value       D xmlHandler      PR            10i 0      D   ignore                       1a      D   termination                       10i 0 value      D   string                        *   value      D   stringLen                   20i 0 value      D   exceptionId                 10i 0 value       D XML             s            500a   varying      D ignoreMe        s              1a       /free           XML = '<xmlTest>+                  <name type="author">AS400 Sample Code/name>+                  </xmlTest>';            xml-sax %handler(xmlHandler: ignoreMe)                %XML(XML: 'doc=string');         *inlr = *on;        /end-free       P xmlHandler      B      D xmlHandler      PI            10i 0      D   ignore                       1a      D   termination                       10i 0 value      D   string                        *   value      D   stringLen                   20i 0 value      D   exceptionId                 10i 0 value       D value           s          65535a   based(String)      D ucs2val         s          16363c   based(String)      D dspstr          s             52a       /free           PrintMe.name = XML_Event_Name(event);          PrintMe.data = *blanks;           select;          when string=*null or stringlen<1;             // no string given...          when stringlen>%size(value);             PrintMe.data = '** string length invalid';          other;             PrintMe.data = %subst(value:1:stringlen);          endsl;           write QSYSPRT PrintMe;          render 0;       /end-free      P                 eastward        *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++       *  Retrieve the termination get upwards for a given termination number.       *+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      P XML_Event_Name  B      D XML_Event_Name  PI            20A   varying      D    termination                      10i 0 value       /free           select;          when termination = *XML_START_DOCUMENT;              render   'XML_START_DOCUMENT';          when termination = *XML_VERSION_INFO;              render   'XML_VERSION_INFO';          when termination = *XML_ENCODING_DECL;              render   'XML_ENCODING_DECL';          when termination = *XML_STANDALONE_DECL;              render   'XML_STANDALONE_DECL';          when termination = *XML_DOCTYPE_DECL;              render   'XML_DOCTYPE_DECL';          when termination = *XML_START_ELEMENT;              render   'XML_START_ELEMENT';          when termination = *XML_CHARS;              render   'XML_CHARS';          when termination = *XML_PREDEF_REF;              render   'XML_PREDEF_REF';          when termination = *XML_UCS2_REF;              render   'XML_UCS2_REF';          when termination = *XML_UNKNOWN_REF;              render   'XML_UNKNOWN_REF';          when termination = *XML_END_ELEMENT;              render   'XML_END_ELEMENT';          when termination = *XML_ATTR_NAME;              render   'XML_ATTR_NAME';          when termination = *XML_ATTR_CHARS;              render   'XML_ATTR_CHARS';          when termination = *XML_ATTR_PREDEF_REF;              render   'XML_ATTR_PREDEF_REF';          when termination = *XML_ATTR_UCS2_REF;              render   'XML_ATTR_UCS2_REF';          when termination = *XML_UNKNOWN_ATTR_REF;              render   'XML_UNKNOWN_ATTR_REF';          when termination = *XML_END_ATTR;              render   'XML_END_ATTR';          when termination = *XML_PI_TARGET;              render   'XML_PI_TARGET';          when termination = *XML_PI_DATA;              render   'XML_PI_DATA';          when termination = *XML_START_CDATA;              render   'XML_START_CDATA';          when termination = *XML_END_CDATA;              render   'XML_END_CDATA';          when termination = *XML_COMMENT;              render   'XML_COMMENT';          when termination = *XML_EXCEPTION;              render   'XML_EXCEPTION';          when termination = *XML_END_DOCUMENT;              render   'XML_END_DOCUMENT';          other;              render   'UNKNOWN EVENT';          endsl;       /end-free      P                 eastward 

Note: It’s of import to empathise that each fourth dimension XML-SAX finds an event, it identifies the termination past times passing a publish to your procedure. For example, each fourth dimension the start of an XML chemical component (often called “tag”) is found, your handler is called amongst event=21. The 21 symbolizes the start of an XML element. RPG provides a named constant for each termination number. In this example, *XML_START_ELEMENT is a named constant that has a value of 21. When you lot run across code that says “if termination = *XML_START_ELEMENT” it’s equivalent to (but to a greater extent than self-documenting than) code that says “if termination = 21.”

In the preceding code, the XML_Event_Name() subprocedure returns a symbolic get upwards for each termination then you lot tin give notice impress out which termination has occurred. The balance of the code prints the information that was passed from XML-SAX to the handler procedure.When you lot run the preceding code, it volition practice a spooled file that contains the following:

XML_START_DOCUMENT                  XML_START_ELEMENT    xmlTest        XML_START_ELEMENT    get upwards           XML_ATTR_NAME        type           XML_ATTR_CHARS       writer         XML_END_ATTR                        XML_CHARS            AS400 Sample Code  XML_END_ELEMENT      get upwards           XML_END_ELEMENT      xmlTest        XML_END_DOCUMENT                 

By all means, give it a try. While you’re at it, endeavour the following:

  1. Try changing the XML string to dissimilar XML values as well as run across what happens.
  2. Try putting an invalid XML document into the XML string, as well as notice how exceptions work.
  3. Try changing the XML-SAX opcode then that it reads information from an XML file inwards the IFS instead of a string inwards your program. 

XML = ‘/home/as400/SampleData.xml’;
xml-sax %handler(xmlHandler: ignoreMe)
%XML(XML: ‘doc=file’);

Click hither for adjacent Chapter

Recommended Reading

Hosting Unlimited Indonesia


Sumber http://www.mysamplecode.com/