Parsing XML in Sahi Scripts
Sahi uses Rhino as its javascript engine and Rhino has excellent support for handling XML.
Below is a script which reads and asserts XML nodes and attributes. The example has been picked from http://www.ibm.com/developerworks/webservices/library/ws-ajax1/ so that it is easy to experiment with the ibm examples in this script.
var xmlStr = '' +'<people>' +' <person gender="male">' +' <name>Ant</name>' +' <hair>Shaggy</hair>' +' <eyes>Blue</eyes>' +' <height measure="metric">176</height>' +' </person>' +' <person gender="male">' +' <name>Paul</name>' +' <hair>Spiky</hair>' +' <eyes>Grey</eyes>' +' <height measure="metric">178</height>' +' </person>' +'</people>';
xmlStr = xmlStr.replace(/<\?xml[^>]*\?>/, ""); // needed in some casesvar $x = new XML(xmlStr);_assertEqual("Ant", $x.person[0].name.toString());_assertEqual("Grey", $x.person[1].eyes.toString());
for each (var $p in $x.person){ var $measure = $p.height.@measure.toString(); _assert($measure == "metric"); _assert($p.height > 170);}Parsing XML containing namespaces
Section titled “Parsing XML containing namespaces”var $xmlStr = '' +'<MyApp xmlns="http://sahi.co.in">' +' <Id xmlns="http://www.tytosoftware.com">123</Id>' +' <Messages>' +' <Welcome>Welcome1 to MyApp</Welcome>' +' <Farewell>Thank1 you for using MyApp</Farewell>' +' </Messages>' +'</MyApp>' ;
var $x = new XML($xmlStr);
// Set the default namespace. All elements will be in this namespacedefault xml namespace = "http://sahi.co.in";var $welcome0 = $x..Welcome[0].toString();_assertEqual("Welcome1 to MyApp", $welcome0);
// Specific namespace for idvar idns = new Namespace("http://www.tytosoftware.com");// Access element using namespace::localNamevar $id = $x.idns::Id.toString();_assertEqual("123", $id);For more details on e4x usage have a look at https://developer.mozilla.org/en-US/docs/E4X/Processing_XML_with_E4X.
There is a lot more that can be done with the XML object. Have a look at these links:
http://www.ibm.com/developerworks/webservices/library/ws-ajax1/
http://www.xml.com/pub/a/2007/11/28/introducing-e4x.html

