Pages

Tuesday, March 15, 2016

Working with XML in RPGLE - Contd.

Working with XML in RPGLE

We have seen how to read XML in RPG by using XML-INTO in the previous post.

If you have not gone through this yet, Click Here to have quick look.

Before we get started with XML-SAX here are some other key points to be noted about XML.

Key Points to Remember:

The XML documents can be in a character or UCS-2 RPG variable, or they can be in an Integrated File System file.
  • This would give freedom to the programmer to choose the best suited form. 

The parser is a SAX (Simple API for XML) parser. A SAX parser operates by reading the XML document character by character.
  • Whenever it has located a fragment of the XML document, such as an element name, or an attribute value, it calls back to a handling procedure provided by the caller of the parser, passing it information about the fragment of XML that it has found.
It'd be easier to understand with an example.

E.g.:

When the parser has found an XML element name, it calls the handling procedure indicating that the "event" is a "start element" event and passing it the name of the element.
The handling procedure processes the information and returns to the parser which continues to read the XML document until it has enough information to call the handling procedure with another event. This process repeats until the entire XML document has been parsed, or until the handling procedure indicates that parsing should end.

Here you go with simple example to see how the events are defined.

<email type="text">
  <sendto>PReddy@email.com</sendto>
</email>

Lets break this data and see how the Parser will read the XML by Events & Event Data.


  • If the Parsed Text is '<email' or '<sendto>', Event would be Start Element and Event Data would be 'email' or 'sendto' respectively.
  • If the Parsed Text is 'type=', Event would be Attribute value and Event Data would be 'type'.
  • If the Parsed Text is 'PReddy@email.com', Event would be Element Content and Event data would be 'PReddy@email.com'.
  • If the Parsed Text is '</email>' or '</sendto>', Event would be End Element and Event Data would be 'email' or 'sendto' respectively.
The XML-SAX and XML-INTO operation codes allow you to use the XML parser. Some more points to make a note about these Opcodes.

  • The XML-SAX operation allows you to specify an event handling procedure to handle every event that the parser generates. This is useful if you do not know in advance what an XML document may contain (XML-INTO will be useful when you know what XML will contain exactly).

E.g.:


    • If you know that an XML document will contain an XML attribute with the name type, and you want to know the value of this attribute, your handling procedure can wait for the "attribute name" event to have a value of "type". Then the next time the handler is called, it should be an "attribute value" event, with the required data ("text" in the example above).

  • The XML-INTO operation allows you to read the contents of an XML document directly into an RPG variable. This is useful if you know the format of the XML document and you know that the names of the XML elements in the document will be the same as the names you have given to your RPG variables.

E.g.:


    • If you know that the XML document will always have the form of the document above, you can define an RPG data structure with the name "email", and with subfields "type" and "sendto".
    • Then you can use the XML-INTO operation to read the XML document directly into the data structure. When the operation is complete, the "type" subfield would have the value "text" and the "sendto" subfield would have the value "PReddy@email.com".


  • The XML-INTO operation also allows you to obtain the values of an unknown number of repeated XML elements. You provide a handling procedure that receives the values of a fixed number of elements each time the handling procedure is called. This is useful if you know that the XML document will contain a series of identical XML elements, but you don't know in advance how many there will be.
  • The XML data is always returned by the parser in text form. If the data is known to represent other datatypes such as numeric data, or date data, the XML-SAX handling procedure must use conversion functions such as %INT or %DATE to convert the data.


We'll see more about XML-SAX & %Handler along with an example in next post. Click Here to go through.

No comments:

Post a Comment

Popular Posts