Home >Web Front-end >JS Tutorial >Processing XML with JavaScript
Today's work involved manipulating an XML string from a textarea using JavaScript's DOM. I recalled Sarissa, an open-source library providing cross-browser compatibility for HTTP requests, XML processing, and XSLT transformations. While powerful (using ActiveX for IE and Mozilla's XML Extras), its 24KB size was a concern for my Mozilla-only application.
Sarissa simplifies XML string to DOM node conversion:
var dom = Sarissa.getDomDocument(); var xml = '<example>This is XML!</example>'; dom.loadXML(xml);
The resulting dom
object is a standard DOM node, manipulated using familiar functions (appendChild, childNodes, etc.). Conversion back to XML is simple:
var xml_again = dom.xml;
To avoid Sarissa's size overhead in my Mozilla-specific context, I examined its source code. The equivalent, more concise Mozilla code directly utilizes the XML Extras package:
var xml = '<example>This is XML!</example>'; var dom = (new DOMParser()).parseFromString(xml, "text/xml");
And the reverse conversion:
var xml_again = (new XMLSerializer()).serializeToString(dom);
Note that official documentation for Mozilla's XML Extras seems limited to its test suite.
The above is the detailed content of Processing XML with JavaScript. For more information, please follow other related articles on the PHP Chinese website!