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.
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>
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.
Download the following files −
Rexxxml − www.interlog.com/~ptjm/
Libxml2 − www.ctindustries.net/libxml/
iconv-1.9.2.win32 − www.xmlsoft.org/sources/win32/oldreleases/
libxslt-1.1.26.win32 − www.xmlsoft.org/sources/win32/oldreleases/
Extract all of the files and ensure they are included in the system path.
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.
This method returns the version of the XML and XSLT libraries used on the system.
xmlVersion()
None
This method returns the version of the XML and XSLT libraries used on the system.
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.
1.0.0 20631 10126
This function is used to parse the XML data sent to the function. The document tree is returned by the function.
xmlParseXML(filename)
Filename − This is the name of the XML file which needs to be parsed.
The document tree is returned by the function. Else returns 0, if there is an error.
rcc = rxfuncadd('XMLLoadFuncs', 'rexxxml', 'xmlloadfuncs') if rcc then do say rxfuncerrmsg() exit 1 end call xmlloadfuncs say xmlVersion() sw = xmlParseXML('test.xml')
No general output.
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.
xmlParseXML(XPath,document)
XPath − This is the path of the node in the xml file.
document − This the XML document
Evaluates XPath expression and returns result as a nodeset which can be used later on.
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.
4
The output shows the number of movie nodes in our xml list
The below method is used to Evaluate an XPath expression and return a string as a result.
xmlParseXML(XPath,Node)
XPath − This is the path of the node in the xml file.
document − The specific node element.
A string is returned based on the XPath expression sent to it.
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.
War, Thriller Anime, Science Fiction Anime, Action Comedy