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