NAME XML.ObjTree -- JavaScript object mapper for XML SYNOPSIS var xotree = new XML.ObjTree(); var tree1 = { root: { node: "Hello, World!" } }; var xml1 = xotree.writeXML( tree1 ); // tree to source alert( "xml1: "+xml1 ); var xml2 = '0'; var tree2 = xotree.parseXML( xml2 ); // source to tree alert( "error: "+tree2.response.error ); DESCRIPTION This class maps JavaScript object for XML. This provides parser/generater methods for XML source code and JavaScript object, and also a wrapper method for XMLHTTPRequest. JavaScript object tree format A sample XML source: Yasuhisa Chizuko Shiori Yusuke Kairi Its JavaScript object tree: { 'family': { '-name': 'Kawasaki', 'father': 'Yasuhisa', 'mother': 'Chizuko', 'children': { 'girl': 'Shiori' 'boy': [ 'Yusuke', 'Kairi' ] } } }; Each elements are parsed into objects: tree.family.father; # the father's given name. Prefix '-' is added on every attributes' name. tree.family["-name"]; # this family's family name A array is used because this family has two boys. tree.family.children.boy[0]; # first boy's name tree.family.children.boy[1]; # second boy's name tree.family.children.girl; # (girl has no other sisiters) METHODS xotree = new XML.ObjTree() This constructor method returns a new XML.ObjTree object. xotree.force_array = [ "rdf:li", "item", "-xmlns" ]; This property allows you to specify a list of element names which should always be forced into an array representation. The default value is null, it means that context of the elements will determine to make array or to keep it scalar. tree = xotree.parseXML( xmlsrc ); This method loads an XML document using the supplied string and returns its JavaScript object converted. tree = xotree.parseDOM( domnode ); This method parses a DOM tree (ex. responseXML.documentElement) and returns its JavaScript object converted. tree = xotree.parseHTTP( url, options ); This method loads a XML file from remote web server and returns its JavaScript object converted. XMLHTTPRequest's synchronous mode is used. This mode blocks the process until the response is completed. First argument is a XML file's URL which must exist in the same domain as parent HTML file's. Cross-domain loading is not available for security reasons. Second argument is options' object which can contains some parameters: method, postBody, parameters, onLoading, etc. This method requires JSAN's HTTP.Request class or prototype.js's Ajax.Request class. xotree.parseHTTP( url, options, callback ); If a callback function is set as third argument, XMLHTTPRequest's asynchronous mode is used. This mode calls a callback function with XML file's JavaScript object converted after the response is completed. xmlsrc = xotree.writeXML( tree ); This method parses a JavaScript object tree and returns its XML source generated. EXAMPLES Text node and attributes If a element has both of a text node and attributes or both of a text node and other child nodes, text node's value is moved to a special node named "#text". var xotree = new XML.ObjTree(); var xmlsrc = 'Kawasaki Yusuke'; var tree = xotree.parseXML( xmlsrc ); var class = tree.span["-class"]; # attribute var name = tree.span["#text"]; # text node parseHTTP() method with HTTP-GET and sync-mode HTTP/Request.js or prototype.js must be loaded before calling this method. var xotree = new XML.ObjTree(); var url = "http://example.com/index.html"; var tree = xotree.parseHTTP( url ); alert( tree.html["-lang"] ); This code shows index.html's lang="" attribute. parseHTTP() method with HTTP-POST and async-mode Third argument is a callback function which is called on onComplete. var xotree = new XML.ObjTree(); var url = "http://example.com/mt-tb.cgi"; var opts = { postBody: "title=...&excerpt=...&url=...&blog_name=..." }; var func = function ( tree ) { alert( tree.response.error ); }; xotree.parseHTTP( url, opts, func ); This code send a trackback ping and shows its response code. Simple RSS reader This is a RSS reader which loads RDF file and displays all items. var xotree = new XML.ObjTree(); xotree.force_array = [ "rdf:li", "item" ]; var url = "http://example.com/news-rdf.xml"; var func = function( tree ) { var elem = document.getElementById("rss_here"); for( var i=0; i