1, Content-Type
In many cases, the Content-Type issue cannot be resolved.
Skip this step if it is an XML file.
The dynamically generated XML must be set to text/XML. Otherwise, text/html is the common text by default.
Set Content-Type in common languages
-
- Header("Content-Type: text/XML");// PHP
-
- Response.Contenttype="Text/XML" // ASP
- Response.Setheader("Contenttype","Text/XML");// JSP
2. XML structure.
XML must be closed. It is very important!
Example:
Incorrect XML
- <?XML Version="1.0" Encoding="UTF-8"?>
-
- <Name>Zhangsan</Name>
- <ID>1</ID>
-
- <Name>Lisi</Name>
-
- <ID>2</ID>
Correct
- <?XML Version="1.0" Encoding="UTF-8"?>
-
- <Stulist>
- <Student Email="1@1.com">
-
- <Name>Zhangsan</Name>
- <ID>1</ID>
-
- </Student>
- <Student Email="2@2.com">
-
- <Name>Lisi</Name>
- <ID>2</ID>
-
- </Student>
-
- </Stulist>
3. Parsing
Here, macnie's
Traverse student (here we still use the above XML and the sub-node is student)
- $.Ajax({
-
- URL:'Ajax. asp',
-
- Type:'Get',
- Datatype:'XML',// Do not write here, but do not write text or HTML !!!
-
- Timeout:1000,
-
- Error:Function(XML){
- Alert('Error loading XML document'+XML);
-
- },
-
- Success:Function(XML){
- $ ( XML ) . Find ( " Student " ) . Each ( function ( I ) {
- var id =$ ( This ) . children ( " id " ) ; // get object
- var idvalue =$ ( This ) . children ( " id " ) . text () ; // retrieve text
- Alert(Id_value);// Here is the ID value.
-
- Alert($(This).ATTR("Email"));// The email attributes under student are displayed here.
-
- // The final output. This is the CSS rain method, which seems to be a little more JQ than macnie.
-
- $('<Li> </LI>')
-
- .Html(Id_value)
- .Appendto('Ol');
-
- });
-
- }
-
- });
The last one is added:Ensure that the server is UTF-8 encoded, otherwise garbled! At the same time, make sure that your xml file is also UTF-8
Format :-)