Home > Web Front-end > JS Tutorial > body text

Convert js string into xml object and use skills to interpret_javascript skills

WBOY
Release: 2016-05-16 17:36:10
Original
1239 people have browsed it

To convert a string into an xml object on the java side, you can use DocumentHelper.parseText(xmlReturn).getRootElement();
There is also a method to convert a string into an xml object in js. You can use the following function
The following is Quote snippet:

Copy code The code is as follows:

function createXml(str){
if(document.all){
var xmlDom=new ActiveXObject("Microsoft.XMLDOM")
xmlDom.loadXML(str)
return xmlDom
}
else
return new DOMParser ().parseFromString(str, "text/xml")
 }

If you read the file on the js side, it will be more convenient
The following is a quote fragment:
Copy code The code is as follows:

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.3.0") ;
xmlDoc.async = false;
Quite convenient.
The following is a quotation fragment:



Copy code The code is as follows: var domxml= createXml (Http.responseText);
var code=domxml.getElementsByTagName("code");
if(code.item(0).text=="100"){
var parameter=domxml.getElementsByTagName ("parameter");
 identifier=parameter.item(0).attributes.getNamedItem("value").value;
 }


For the value of the node and the value of the attribute The methods of obtaining are different.
The following method



Copy code The code is as follows: //The string is converted to xml
function toXmlDom(source){
var xmlDoc = null;
if (window.ActiveXObject) {
var ARR_ACTIVEX =
["MSXML4.DOMDocument","MSXML3.DOMDocument", "MSXML2.DOMDocument","MSXML.DOMDocument","Microsoft.XmlDom"];
var XmlDomflag = false;
for (var i = 0;i < ARR_ACTIVEX.length && !XmlDomflag ;i ) {
try {
var objXML = new ActiveXObject(ARR_ACTIVEX[i]);
xmlDoc = objXML;
XmlDomflag = true;
} catch (e) {
}
}
if (xmlDoc) {
xmlDoc.async = false;
xmlDoc.loadXML(source);
}
}else{
var parser=new DOMParser();
var xmlDoc=parser.parseFromString(source,"text/xml");
}
return xmlDoc;
}




Copy code The code is as follows: //Use
function areaChart(data){
var s = toXmlDom(xml);/ /xml is a string
$(s).find("area").each( //Get each area tag
function(id,item){
var areaCode=$(item). find("area_code").eq(0).text();//Get the content of the area tag
var num = $(item).find("area_all_num").eq(0).text();
var name=$(item).find("area_name").eq(0).text();
var title=name "," num;
$("#" areaCode "" ).attr("title",title);
}
);



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template