JS Operation XML Instance summary (load and parse XML file, string) _javascript skill

Source: Internet
Author: User
Tags xpath

This article illustrates the method of JS operation XML. Share to everyone for your reference, specific as follows:

My XML file is login.xml as follows.

<?xml version= "1.0" encoding= "Utf-8"?>
<Login>
 <Character>
 <c text= "Blood" value= "0" ></C>
 <c text= "weak gas" value= "1" ></C>
 <c text= "Passion" value= "2" ></C>
 <c text= "Cool" value= "3" ></C>
 <c text= "Cold" value= "4" ></C>
 </Character>
 < weapon>
 <w text= "Beam sword" value= "0" ></W>
 <w text= "beam matching knife" value= "1" ></W>
 </ weapon>
 <EconomyProperty>
 <p text= "average type" value= "0" ></P>
 <p text= "value attack" value = "1" ></P>
 <p text= "value Agile" value= "2" ></P>
 <p text= "value Defense" value= "3" ></P>
 <p text= "value hit" value= "4" ></P>
 </EconomyProperty>
</Login>

Now I need to manipulate the contents of this XML file.

First of all, we need to load this XML file, JS load XML file, is through the XMLDOM.

Load XML document
Loadxml = function (xmlFile)
{
 var xmldoc;
 if (window. ActiveXObject)
 {
 xmldoc = new ActiveXObject (' Microsoft.XMLDOM ');
 Xmldoc.async = false;
 Xmldoc.load (XmlFile);
 }
 else if (document.implementation&&document.implementation.createdocument)
 {
 xmldoc = Document.implementation.createDocument (",", null);
 Xmldoc.load (XmlFile);
 }
 else
 {return
 null;
 }
 return xmldoc;
}

The XML file object comes out, and then I'm going to work on this document.

For example, we now need to get the properties of the first node login/weapon/w node, so we can do the following.

First, the XML object is judged
checkxmldocobj = function (xmlFile)
{
 var xmldoc = Loadxml (xmlFile);
 if (xmldoc==null)
 {
 alert (' Your browser does not support XML file reads, so this page prohibits your operation, the recommended use of IE5.0 above can solve this problem! ');
 window.location.href= '/index.aspx ';
 }
 return xmldoc;
}
And then start to get the desired attribute value of the first node of the login/weapon/w
var xmldoc = checkxmldocobj ('/ebs/xml/login.xml ');
var v = xmldoc.getelementsbytagname (' login/weapon/w ') [0].childnodes.getattribute (' Text ')

And I write in my program is like this, of course, I am in the process of writing is already applied to the actual. and give out to see

Initializeselect = function (OID, XPath)
{
 var xmldoc = checkxmldocobj ('/ebs/xml/login.xml ');
 var N;
 var L;
 var e = $ (OID);
 if (e!=null)
 {
 n = xmldoc.getelementsbytagname (XPath) [0].childnodes;
 L = n.length;
 for (var i=0 i<l; i++)
 {
  var option = document.createelement (' option ');
  Option.value = N[i].getattribute (' value ');
  option.innerhtml = N[i].getattribute (' Text ');
  E.appendchild (option);}}


In the access code above, we do this through Xmldoc.getelementsbytagname (XPath).
You can also pass XmlDoc.documentElement.childNodes (1). ChildNodes (0). getattribute (' Text ') is accessed.

Some common methods:

XmlDoc.documentElement.childNodes (0). nodename, you can get the name of this node.
XmlDoc.documentElement.childNodes (0). NodeValue, you can get the value of this node. This value is derived from this XML format: <A>B</B>, so you can get the value of B.
XmlDoc.documentElement.childNodes (0). Haschild, you can determine whether there are child nodes
in my experience, it is best to use the getElementsByTagName (XPath) approach to access the nodes, as this can be done directly through XPath to locate the node, which would have better performance .

Add:

JS Parse XML file

<script type= ' text/javascript ' >
 loadxml = function (xmlFile) {
 var xmldoc=null;
 Determine the type of browser
 //support IE browser
 if (!window. Domparser && windows. ActiveXObject) {
  var xmldomversions = [' msxml.2.domdocument.6.0 ', ' msxml.2.domdocument.3.0 ', ' microsoft.xmldom '] ;
  for (Var i=0;i<xmldomversions.length;i++) {
  try{
   xmldoc = new ActiveXObject (xmldomversions[i));
   break;
  catch (E) {
  }
  }
 }
 //support Mozilla browser
 else if (document.implementation && document.implementation.createDocument) {
  try{
  /* document.implementation.createDocument (",", null); Three parameter description of the method
   * The first parameter is a string containing the namespace URI used by the document; 
   * The second parameter is a string containing the name of the document root element; 
   * The third parameter is the type of document to create (also known as DOCTYPE).
  xmldoc = Document.implementation.createDocument (",", null);
  } catch (E) {
  }
 }
 else{return
  null;
 }
 if (xmldoc!=null) {
  Xmldoc.async = false;
  Xmldoc.load (XmlFile);
 }
 return xmldoc;
 }
</script>

JS Parse XML string

<script type= ' text/javascript ' > loadxml = function (xmlstring) {var xmldoc=null; Determine the type of browser//support IE browser if (!window. Domparser && windows. ActiveXObject) {//window. Domparser to determine if the non-IE browser var xmldomversions = [' msxml.2.domdocument.6.0 ', ' msxml.2.domdocument.3.0 ', ' microsoft.xmldom ']
  ;
   for (Var i=0;i<xmldomversions.length;i++) {try{xmldoc = new ActiveXObject (xmldomversions[i));
   Xmldoc.async = false; Xmldoc.loadxml (xmlstring);
  The Loadxml method loads an XML string break; }catch (e) {}}}//support Mozilla Browser else if window. Domparser && document.implementation && document.implementation.createDocument) {try{/* Domparser Object Solution
   Analyzes the XML text and returns an XML Document object. * To use Domparser, instantiate it with a constructor without parameters, and then call its parsefromstring () method * parsefromstring (text, contentType) parameter text: XML tag parameter C to parse The content type of the Ontenttype text may be one of the "Text/xml", "Application/xml" or "Application/xhtml+xml".
   Note that "text/html" is not supported.
  * * Domparser = new Domparser (); xmldoc = Domparser.parsefromstrinG (xmlstring, ' text/xml ');
 }catch (e) {}} else{return null;
 return xmldoc;

 } </script>

Test XML

<?xml version= "1.0" encoding= "Utf-8"?>
<DongFang>
 <Company>
 <cnname>1</ cnname>
 <cIP>1</cIP>
 </Company>
 <Company>
 <cnname>2</ cnname>
 <cIP>2</cIP>
 </Company> 
 <Company>
 <cnname>3</ cnname>
 <cIP>3</cIP>
 </Company>
 <Company>
 <cnname>4</ cnname>
 <cIP>4</cIP>
 </Company>
 <Company>
 <cnname>5</ cnname>
 <cIP>5</cIP>
 </Company>
 <Company>
 <cnname>6</ cnname>
 <cIP>6</cIP>
 </Company>
</DongFang>

How to use:

var xmldoc=loadxml (text.xml)
var elements = xmldoc.getelementsbytagname ("Company");
for (var i = 0; i < elements.length i++) {
 var name = Elements[i].getelementsbytagname ("Cnname") [0].firstchild.nod Evalue;
 var ip = elements[i].getelementsbytagname ("CIP") [0].firstchild.nodevalue;
}

I hope this article will help you with JavaScript programming.

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.