JavaScript parsing of XML

Source: Internet
Author: User

1. Parsing the responsexml returned by the XMLHttpRequest request, Responsexml is a XmlDocument object
Suppose the returned Responsexml is:
<?xml version= "1.0" encoding= "UTF-8"
Standalone= "Yes"?>
<response>
<method>checkName</method>
<result>1</result>
</response>
The method and result values are obtained by:
var response=req.responsexml.documentelement;
Method =response.getelementsbytagname (' method ') [0].firstchild.data;
result = Response.getelementsbytagname (' result ') [0].firstchild.data;

2. Create a XmlDocument object
function GetXmlDocument () {
var xdoc = null;
if (document.implementation && document.implementation.createDocument) {
Xdoc = Document.implementation.createDocument ("", "", null);
} else {
if ((typeof ActiveXObject)!= "undefined") {
var msxmlax = null;
try {
Msxmlax = new ActiveXObject ("Msxml2.domdocument");
}
catch (e) {
Msxmlax = new ActiveXObject ("Msxml.domdocument");
}
Xdoc = Msxmlax;
}
}
if (Xdoc = null | | typeof xdoc.load = = "undefined") {
Xdoc = null;
}
return xdoc;
}

3. Create a DOM tree
<people>
<person first-name= "Eric" Middle-initial= "H" last-name= "Jung" >
<address street= "321 South St" city= "Denver" state= "CO" country= "USA"/>
</person>
<person first-name= "Jed" Last-name= "Brown" >
<address street= "321 North St" city= "Atlanta" state= "GA" country= "USA"/>
<address street= "321 South Avenue" City= "Denver" state= "CO" country= "USA"/>
</person>
</people>
The procedure is as follows:
var doc=getxmldocument ();
var Peopleelem = doc.createelement ("people");
var personElem1 = doc.createelement ("person");
Personelem1.setattribute ("First-name", "Eric");
Personelem1.setattribute ("Middle-initial", "H");
Personelem1.setattribute ("Last-name", "Jung");

var addressElem1 = doc.createelement ("Address");
Addresselem1.setattribute ("Street", "321 South St");
Addresselem1.setattribute ("City", "Denver");
Addresselem1.setattribute ("state", "co");
Addresselem1.setattribute ("Country", "USA");
Personelem1.appendchild (ADDRESSELEM1);

var personElem2 = doc.createelement ("person");
Personelem2.setattribute ("First-name", "Jed");
Personelem2.setattribute ("Last-name", "Brown");

var addressElem3 = doc.createelement ("Address");
Addresselem3.setattribute ("Street", "321 North St");
Addresselem3.setattribute ("City", "Atlanta");
Addresselem3.setattribute ("state", "GA");
Addresselem3.setattribute ("Country", "USA");
Personelem2.appendchild (ADDRESSELEM3);

var addressElem5 = doc.createelement ("Address");
Addresselem5.setattribute ("Street", "321 South Avenue");
Addresselem5.setattribute ("City", "Denver");
Addresselem5.setattribute ("state", "co");
Addresselem5.setattribute ("Country", "USA");
Personelem2.appendchild (ADDRESSELEM5);

Peopleelem.appendchild (PERSONELEM1);
Peopleelem.appendchild (PERSONELEM2);
Doc.appendchild (Peopleelem);
alert (doc.xml);//xml property only works with IE

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.