XML and JavaScript instances

Source: Internet
Author: User

XML and JavaScript instances

Brief introduction:

Browser problem:

When it comes to browser-incompatible client language is a major problem. But here we have to use XML and Javascript,xml is the problem. Not all browsers have support for parsing XML documents. I will use IE6 to interpret the code. The browser does not support XML and cannot be read. When you are in a browser such an XML file, you will just ignore all the tags.

Sample XML file:

<company>
<employee id= "001" sex= "M" age= ">premshree pillai</employee>"
<employee id= "002" sex= "M" age= "a" >kumar singh</employee>
<employee id= "003" sex= "M" age= ">ranjit"
<turnover>
<year id= ">100,000</year>"
<year id= "2001" >140,000</year>
<year id= "2002" >200,000</year>
</turnover>
</company>


The above XML file shows employee data and turnover of the company (just a e.g).
Manipulating XML file data using JavaScript:


Loading XML file data using Javascript
var xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
function Loadxml (xmlFile)
{
Xmldoc.async= "false";
xmldoc.onreadystatechange=verify;
Xmldoc.load (XmlFile);
Xmlobj=xmldoc.documentelement;
}


In fact, only the last two lines of functionality are sufficient to load an XML file. The two preceding lines are written to ensure that the JavaScript function, which we can use later to manipulate the data in the XML file, is an uninitialized object that does not perform any function. Therefore, functional validation () is invoked.

function Verify ()
{
0 Object is not initialized
1 Loading object is Loading data
2 Loaded object has Loaded data
3 Data from Object can is worked with
4 Object completely initialized
if (xmldoc.readystate!= 4)
{
return false;
}
}


Now the XML file can is loaded

Loadxml (' Xml_file.xml ');

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.