JS string into XML object and use techniques to interpret _javascript techniques

Source: Internet
Author: User
converting strings to XML objects on the Java side can use Documenthelper.parsetext (Xmlreturn). Getrootelement ();
In JS there is also a way to convert strings into XML objects, you can use the following functions
The following is a reference fragment:
Copy Code code 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 are reading a file on the JS side, it is more convenient
The following is a reference fragment:
Copy Code code as follows:

var xmldoc = new ActiveXObject ("msxml2.domdocument.3.0");
Xmldoc.async = false;
Xmldoc.load ("File path");

As for the operation of XML is also very simple, if you can use Jdom or dom4j, the operation is quite convenient.
The following is a reference fragment:
Copy Code code 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 value of the node and the value of the property are fetched differently.
The following method
Copy Code code 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 Code code as follows:

Use
function AreaChart (data) {
var s = toxmldom (XML);//xml is a string
$ (s). Find (' area '). each (///Get every area label
function (Id,item) {
var areacode=$ (item). FIND ("Area_code"). EQ (0). text ();//Get the contents of an area label
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);
}
);

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.