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