Convert JavaScript strings into xml objects and use skills

Source: Internet
Author: User

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 );
}
);

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.