Rexx - XML


Advertisements


XML is a portable, open source language that allows programmers to develop applications that can be read by other applications, regardless of the operating system and/or developmental language. This is one of the most common languages used for exchanging data between applications.

What is XML?

The Extensible Markup Language XML is a markup language much like HTML or SGML. This is recommended by the World Wide Web Consortium and available as an open standard. XML is extremely useful for keeping track of small to medium amounts of data without requiring a SQL-based backbone.

For all our XML code examples, let's use the following simple XML file movies.xml for construction of the XML file and reading the file subsequently.

<collection shelf = "New Arrivals"> 
   <movie title = "Enemy Behind"> 
      <type>War, Thriller</type> 
      <format>DVD</format> 
      <year>2003</year> 
      <rating>PG</rating> 
      <stars>10</stars> 
      <description>Talk about a US-Japan war</description> 
   </movie> 
   
   <movie title = "Transformers"> 
      <type>Anime, Science Fiction</type> 
      <format>DVD</format> 
      <year>1989</year> 
      <rating>R</rating> 
      <stars>8</stars> 
      <description>A schientific fiction</description> 
   </movie> 
   
   <movie title = "Trigun"> 
      <type>Anime, Action</type> 
      <format>DVD</format> 
      <year>1986</year> 
      <rating>PG</rating> 
      <stars>10</stars> 
      <description>Vash the Stam pede!</description> 
   </movie> 
   
   <movie title = "Ishtar"> 
      <type>Comedy</type> 
      <format>VHS</format> 
      <year>1987</year> 
      <rating>PG</rating> 
      <stars>2</stars> 
      <description>Viewable boredom </description> 
   </movie> 
</collection>

Getting Started

By default, the xml functionality is not included in the Rexx interpreter. In order to work with XML in Rexx, the following steps need to be followed.

Loading XML Functions

Once all the files in the above section have been downloaded and successfully registered, the next step is to write the code to load the Rexx XML functions. This is done with the following code.

rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') 

if rcc then do 
   say rxfuncerrmsg() 
   exit 1 
end 
call xmlloadfuncs

The following things can be noted about the above program −

  • The function rxfuncadd is used to load external libraries. The xmlloadfuncs function is used to load all the libraries in the rexxxml file into memory.

  • If the value of rcc<>0, then it would result in an error. For this , we can call the rxfuncerrmsg to give us more details on the error message.

  • We finally make a call to xmlloadfuncs, so that all xml related functionality can now be enabled in the Rexx program.

Let’s look at the various methods available for XML in Rexx.

xmlVersion

This method returns the version of the XML and XSLT libraries used on the system.

Syntax

xmlVersion()

Parameters

None

Return Value

This method returns the version of the XML and XSLT libraries used on the system.

Example

rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') 

if rcc then do 
   say rxfuncerrmsg() 
   exit 1 
end 
call xmlloadfuncs 
say xmlVersion() 

When we run above program we will get the following result. This again depends on the version of the XML libraries being used on the system.

Output

1.0.0 20631 10126 

xmlParseXML

This function is used to parse the XML data sent to the function. The document tree is returned by the function.

Syntax

xmlParseXML(filename) 

Parameters

  • Filename − This is the name of the XML file which needs to be parsed.

Return Value

The document tree is returned by the function. Else returns 0, if there is an error.

Example

rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') 

if rcc then do 
   say rxfuncerrmsg() 
   exit 1 
end 
call xmlloadfuncs 

say xmlVersion() 
sw = xmlParseXML('test.xml') 

Output

No general output.

xmlFindNode

This method evaluates the XPath expression passed to it. This is used for parsing the document tree to result a nodeset which can be processed further.

Syntax

xmlParseXML(XPath,document) 

Parameters

  • XPath − This is the path of the node in the xml file.

  • document − This the XML document

Return Value

Evaluates XPath expression and returns result as a nodeset which can be used later on.

Example

rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') 

if rcc then do 
   say rxfuncerrmsg() 
   exit 1 
end 
call xmlloadfuncs

say xmlVersion() 
document = xmlParseXML('test.xml') 
nodeset = xmlFindNode('//movie', document) 
say xmlNodesetCount(nodeset)

When we run above program we will get the following result.

Output

4

The output shows the number of movie nodes in our xml list

xmlEvalExpression

The below method is used to Evaluate an XPath expression and return a string as a result.

Syntax

xmlParseXML(XPath,Node) 

Parameters

  • XPath − This is the path of the node in the xml file.

  • document − The specific node element.

Return Value

A string is returned based on the XPath expression sent to it.

Example

rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') 

if rcc then do 
   say rxfuncerrmsg() 
   exit 1 
end 
call xmlloadfuncs 

document = xmlParseXML('test.xml') 
nodeset = xmlFindNode('//movie', document) 
do j = 1 to xmlNodesetCount(nodeset) 
value = xmlEvalExpression('type', xmlNodesetItem(nodeset, j)) 
say value 
end

When we run above program we will get the following result.

Output

War, Thriller 
Anime, Science Fiction 
Anime, Action 
Comedy 


Advertisements
E-Books Store