JavaScript reads XML files

Source: Internet
Author: User

What is XML?
    • XML refers to Extensible Markup Language (extensible Markup Language)
    • XML is a markup language, much like HTML
    • XML is designed to transmit data rather than display it
    • XML tags are not predefined. You need to define your own labels.
    • XML is designed to be self-descriptive.
    • XML is the recommended standard for the consortium

XML file

<Note>    <ID>1</ID>    <name>Sevennight</name>    <Sex>Man</Sex>    <style>12</style></Note>

1. Parse the XML document into an XML DOM object:

if (Window. XMLHttpRequest) {//  code for ie7+, Firefox, Chrome, Opera, Safari    xmlhttp=new  Else {//  code for IE6, IE5    xmlhttp=new ActiveXObject ("Microsoft.XMLHTTP");} Xmlhttp.open ("GET", "Test.xml",false); Xmlhttp.send (); xmldoc=xmlhttp.responsexml;

Read node
Xmldoc.getelementsbytagname ("id") [0].childnodes[0].nodevalue;

Xmldoc.getelementsbytagname ("name") [0].childnodes[0].nodevalue;

Xmldoc.getelementsbytagname ("Sex") [0].childnodes[0].nodevalue;

Xmldoc.getelementsbytagname ("style") [0].childnodes[0].nodevalue;

Cross-domain Access

For security reasons, modern browsers do not allow cross-domain access.

This means that the Web page and the XML file it is trying to load must be on the same server, on the same domain

If you are going to use the above example on your own web page, you must put the XML file on your server. Otherwise, xmldoc.load () will produce the error "Access is denied".

If you want to no longer have access on the same domain, you can also set allow cross-domain access on the server's corresponding header

Indicates that any Web site can request
Response.setheader ("Access-control-allow-origin", "*");

Indicates that only http://www.baidu.com can request
Response.setheader ("Access-control-allow-origin", "http://www.baidu.com");

Access response Information

http/1.1 okset-cookie:ao= "O=1&s=1&dnt=1"; Version=1; domain=yahoo.com; max-age=630720000; Expires=sat, 18-jun-2033 10:07:41 GMT; path=/Access-control-allow-origin: *    // indicates the appropriate header to allow cross-domain public, max-age=899  Content-type:text/xml;charset=utf-8content-Encoding:gzipVary:Accept-June 2013 10:07:40 GMT

In HTML, you can set the

<meta http-equiv= "Access-control-allow-origin" content= "*" ><meta http-equiv= "Access-Control-Allow-Origin "Content=" > "http://www.baidu.com"

JavaScript reads an XML file

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.