String对象与xml文档对象的相互转换

1.string转换为xml
function stringToXml(xmlString)
{
   var xmlDoc;
   if(typeof xmlString == "string")
   {
	//FF
	if (document.implementation.createDocument) {
	     var parser = new DOMParser()
	     xmlDoc = parser.parseFromString(xmlString, "application/xml")
	//IE
	} else if (window.ActiveXObject) {
	     xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
	     xmlDoc.async="false"
	     xmlDoc.loadXML(xmlString)
	}	
  }
  else
  {	
        xmlDoc = xmlString;
   }
   return xmlDoc;
}


2.xml转换为string
function xmlToString()
{
	if (window.ActiveXObject) {   
		return this.xmlDoc.xml;  //IE
	} else {  
		return (new XMLSerializer()).serializeToString(this.xmlDoc);  //FF
	}	
}

你可能感兴趣的:(JavaScript,xml,IE,Microsoft)