Use DOM to operate XML

Source: Internet
Author: User
When using AJAX, although the JSON data format is simple and easy to use, the server-side language does not support JSON as well as XML. Sometimes we need to return the XML data format from the background to the foreground, therefore, it is necessary to learn how javascript uses DOM to operate XML. The following section describes how javascript uses DOM to operate X... syntaxHighlighter. when using AJAX, although the JSON data format is simple and easy to use, the server-side language does not support JSON as well as XML. Sometimes we need to return the XML data format from the background to the foreground, therefore, it is necessary to learn how to use javascript to operate XML using DOM. The following code uses javascript to operate XML 1. Loading XML documents [javascript] function loadXMLDocument (flag, xmlDoc) {// flag = true indicates the XML document, otherwise it is an XML string // if (window. activeXObject) {var CtiveName = ['msxml2. DOMDocument ', 'Microsoft. xmlDom ']; var xmlObj; for (var I = 0; I try {xmlObj = new ActiveXObject (activeName [I]); break;} catch (e) {}}// object created successfully if (xmlObj) {// load XML document xmlObj in synchronous mode. async = false; // load the xml document if (flag) {xmlObj. load (xmlDoc);} else {// load the xml string xmlObj. loadXML (xmlDoc);} // return the Root Node object // return xmlObj; // return the root element object return xmlObj.doc umentElement ;} else {// object creation failed alert ("xmlbeannet object creation failed"); re Turn null;} // FireFox Loading Method} else if (document. implementation. createDocument) {var xmlObj; if (flag) {// load xml document xmlObj = document. implementation. createDocument ("", "", null); if (xmlObj) {xmlObj. async = false; xmlObj. load (xmlDoc); return xmlObj // return xmlObj.doc umentElement;} else {alert ("failed to create object"); return null ;}} else {// load the xml string xmlObj = new DOMParser (); var docRoot = xmlObj. parseFromString (xmlDoc, 'text/x Ml '); return docRoot.doc umentElement;} else {alert ("xmlbeannet object creation failed"); return null ;}2. After the XML file is loaded, the rest is how to use the XPATH technology to get the content in XML. Here we usually use two methods: selectNodes () and selectSingleNode () 2.1 selectSingleNode () method [javascript] function selectSingleNode (xmlDom, sXpath) {if (window. activeXObject) {// IE browser return xmlDom. selectSingleNode (sXpath);} else if (window. XPathEvaluator) {// firefox browser var xpathObj = new xpatev Aluator (); if (xpathObj) {var result = xpathObj. evaluate (sXpath, xmlDom, null, XPathResult. FIRST_ORDERED_NODE_TYPE, null); return result. singleNodeValue ;}else {return null ;}} else {return null ;}} 2.2 selectNodes () method [javascript] function selectNodes (xmlDom, sXpath) {if (window. activeXObject) {// IE browser return xmlDom. selectNodes (sXpath);} else if (window. XPathEvaluator) {// firefox browser var xpathObj = new xpatev Aluator (); if (xpathObj) {var result = xpathObj. evaluate (sXpath, xmlDom, null, XPathResult. ORDERED_NODE_ITERATOR_TYPE, null); var nodes = new Array (); var node; while (node = result. iterateNext ())! = Null) {nodes. push (node) ;}return nodes ;}else {return null ;}} else {return null ;}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.