XmlParser

=XmlParser= toc

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.

[|XML information from Wikipedia:] //The Extensible Markup Language (XML) is a general-purpose markup language. It is classified as an extensible language because it allows its users to define their own tags. Its primary purpose is to facilitate the sharing of structured data across different information systems, particularly via the Internet.//

Syntax
Syntax for the XMLPARSER statement from the Run BASIC helpfile.

XMLPARSER #handle, docExpr$ //- Create an XML parser object on docExpr$.//

XmlParser Methods
XMLPARSER methods


 * 1) handle ELEMENTCOUNT //- Return the number of child XML elements//
 * 2) handle KEY$ //- Return the key as a string from an XML expression like value //
 * 3) handle VALUE$ //- Return the value as a string from an XML expression like value //
 * 4) handle VALUEFORKEY$(keyExpr$) //- Return the value for the specified tag key in keyExpr$//
 * 5) handle #ELEMENT(n) //- Return the nth child-element XML element//
 * 6) handle #ELEMENT(nameExpr$) //- Return the child-element XML element named by nameExpr$//
 * 7) handle ATTRIBCOUNT //- Return a count of attribute pairs;  has two pairs//
 * 8) handle ATTRIBKEY$(n) //- Return the key string of the nth attribute//
 * 9) handle ATTRIBVALUE$(n) //- Return the value string of the nth attribute//
 * 10) handle ATTRIBVALUE$(n$) //- Return the value string of the attribute with the key n$, or an empty string if it doesn't exist.//
 * 11) handle ISNULL //- Returns zero (or false)//
 * 12) handle DEBUG$ //- Returns the string "Xmlparser"//

Error Handling
If the XML parser detects poorly formed XML when it parses it will throw a runtime error. If you trust that the XML provided is well formed you may decide that you don't need an error handler, but otherwise you should use ON ERROR GOTO to set one up. Look at the httpget.bas example provided with Run BASIC for an example of how to do this.

Demos
Parsing a simple XML string:

code format="vb" ' The xml string xml$ = " Sam  Clover  Alex  "

' Creates the xml handler, using the string xmlparser #spies, xml$

' Uses elementCount to know how many elements are in betweeb ... for count = 1 to #spies elementCount

' Uses "count" to work through the elements, and assigns the element to the ' handle "#spy" #spy = #spies #element(count)

' Prints the value, or inner text, of "#spy": Sam, Clover, & Alex print #spy value$

next count

end code

Parsing a simple XML string with attributes:

code format="vb" ' The xml string, this time each "spy" element has an attribute named "color" xml$ = " Sam  Clover " _ + " Alex "

' Creates the xml handler, using the string xmlparser #spies, xml$

' Uses elementCount to know how many elements are in between ... for count = 1 to #spies elementCount

' Uses "count" to work through the elements, and assigns the element to the ' handle "#spy" #spy = #spies #element(count)

' Prints the value, or inner text, of "#spy": Sam, Clover, & Alex print #spy value$;

' If "#spy" has an attribute named "color" with a value, it is printed out ' after the name. Colors for this example are: green, red, & yellow if #spy attribValue$("color") <> "" then

' attribValue$ is the function used to extract an attributes value ' from an element handle, in this case "#spy" print " has a " + #spy attribValue$("color") + " suit.";

end if

print ""

next count

end code