Parsing XML

Jump to Run BASIC Help File

Getting an XML accessor
Accessing element attributes
Error handling
XMLPARSER methods

Run BASIC includes a non-validating XML parser.  This can useful for various things including working with information that you retrieve from the web using Run BASIC's httpget function.

Getting an XML accessor

Use get an XML accessor object, use the XMLPARSER statement.  You specify an object variable name and a string expression, like so:

xmlparser #accessor, "<someTag>attr=""some text""</someTag>"

The resulting parsed XML can be read from the object in #accessor.  Here's an example that reads some simple XML (line breaks are allowed in RB string literals):

xml$ = "<group>
    <customer>
        <name>Fred Jones</name>
        <email>fj@mail.com</email>
        <phone>234-3456</phone>
    </customer>
    <customer>
        <name>Ed Black</name>
        <email>eb@mail.com</email>
        <phone>123-2345</phone>
    </customer>
</group>"
xmlparser #accessor, xml$
print "Contents of <"+#accessor key$()+">"
for i = 1 to #accessor elementcount()
  #customer = #accessor #element(i)
  print "Customer: ";
  print #customer valueforkey$("name"); ", ";
  print #customer valueforkey$("email"); ", ";
  print #customer valueforkey$("phone")
next i

The output of this example is:

Contents of <group>
Customer: Fred Jones, fj@mail.com, 234-3456
Customer: Ed Black, eb@mail.com, 123-2345

When the XMLPARSER statement is executed in the example, the #accessor object is returned on the highest level XML element <group>.  Notice that we used the KEY$() method to get the name of that element to print it out.

Then used the ELEMENTSCOUNT() method and a FOR/NEXT loop to access the two <customer> elements.  The VALUEFORKEY$() method is used to get the string value for each of the contained tags <name>, <email>, and <phone>.

Since we have the #customer object, we could also use the ELEMENTSCOUNT() and ELEMENT() methods to access each tag as an object in this fashion which produces identical output as the code above:

for i = 1 to #accessor elementcount()
  #customer = #accessor #element(i)
  print "Customer: ";
  for c = 1 to #customer elementcount()
    #item = #customer #element(c)
    print #item value$();
    if c < #customer elementcount() then print ", ";
  next c
  print
next i

Accessing element attributes

XML elements can also have attibutes in addition to tag names and values.  For example a common tag seen in web page source code:

  <a href = "http://www.runbasic.com" target="_blank">The Run BASIC site</a>

To read the HREF and TARGET attributes you can use the ATTRIBCOUNT(), ATTRIBKEY$(), ATTRIBVALUE$() methods.  Here is an example:

xml$="<a href = ""http://www.runbasic.com"" target=""_blank"">
The Run BASIC site
</a>"
xmlparser #href, xml$
for x = 1 to #href attribcount()
  print #href attribkey$(x);", "; #href attribvalue$(x)
next x

Error handling

If the XMLPARSER statement attempts to parse malformed XML it will throw a runtime error, bringing your web application to a halt.  The way to deal with this is to use ON ERROR GOTO to set up an error handler.  Here is an example:

  xml$="<a href = ""http://www.runbasic.com"" target=""_blank"">
The Run BASIC site"

[parseIt]
  on error goto [parsingFailed]
  xmlparser #href, xml$
  for x = 1 to #href attribcount()
  print #href attribkey$(x);", "; #href attribvalue$(x)
  next x
  end

[parsingFailed]
  print Err$
  print "Adding </a> tag and retrying."
  xml$ = xml$ + "</a>"
  goto [parseIt]

XMLPARSER methods

#handle ELEMENTCOUNT() - Return the number of child XML elements
#handle KEY$() - Return the key as a string from an XML expression like <key>value</key>
#handle VALUE$() - Return the value as a string from an XML expression like <key>value</key>
#handle VALUEFORKEY$(keyExpr$) - Return the value for the specified tag key in keyExpr$
#handle #ELEMENT(n) - Return the nth child-element XML element
#handle #ELEMENT(nameExpr$) - Return the child-element XML element named by nameExpr$
#handle ATTRIBCOUNT() - Return a count of attribute pairs;  <a attrA="abc" attrB="def"> has two pairs
#handle ATTRIBKEY$(n) - Return the key string of the nth attribute
#handle ATTRIBVALUE$(n) - Return the value string of the nth attribute
#handle ATTRIBVALUE$(n$) - Return the value string of the attribute with the key n$, or an empty string if it doesn't exist.
#handle ISNULL() - Returns zero (or false)
#handle DEBUG$() - Returns the string "Xmlparser"