To convert a string to an xml object on the java end, use incluenthelper. parseText (xmlReturn). getRootElement ();
In js, there are also ways to convert strings into xml objects. You can use the following functions:
The following is a reference clip:
Copy codeThe 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 ")
}
It is more convenient to read files on the js end.
The following is a reference clip:
Copy codeThe Code is as follows:
Var xmlDoc = new ActiveXObject ("Msxml2.DOMDocument. 3.0 ");
XmlDoc. async = false;
XmlDoc. load ("file path ");
Xml operations are also very simple. If jdom or dom4j is used, the operations are quite convenient.
The following is a reference clip:
Copy codeThe 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;
}
The method for obtaining node values and attribute values is different.
The following Method
Copy codeThe Code is as follows:
// Convert string 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 codeThe Code is as follows:
// Use
Function areaChart (data ){
Var s = toXmlDom (xml); // xml is a string
$ (S). find ("area"). each (// obtain the label of each area
Function (id, item ){
Var areaCode = $ (item). find ("area_code"). eq (0). text (); // obtain the area TAG content
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 );
}
);