First scenario:
Copy Code code as follows:
<script type= "Text/javascript" >
$ (document). Ready (function () {
$.ajax ({
url: ' Http://www.test.net/cgi/test.xml ',
datatype: ' xml ',
success:function (data) {
//console.log (data);
$ (data). Find ("Channel"). Find ("item"). Each (function (index, ele) {
var titles = $ (ele). Find ("title"). Text ();
var links = $ (ele). Find ("link"). text ();
console.log (titles+ '-----');
$ ("#noticecon"). Find (' ol '). Append (' <li><a href= "' +links+ '" > ' +titles+ ') </a></li> ');
});
}
});
})
</script>
<div id= "Noticecon" >
<ol>
&NBSP;&N Bsp;</ol>
</div>
The second option:
Copy Code code as follows:
<script type= "Text/javascript" >
$.get ("Http://www.test.net/cgi/test.xml", function (data) {
$ (data). Find (' channel '). Find (' item '). Each (function (index, ele) {
var titles = $ (ele). Find (' title '). Text ();
var links = $ (ele). Find (' link '). text ();
$ ("#noticecon"). Find (' ol '). Append (' <li><a href= "' +links+ '" > ' +titles+ ' </a></li> ');
})
});
</script>
<div id= "Noticecon" >
<ol>
</ol>
</div>
The general steps are as follows:
1. Reading XML files
Copy Code code as follows:
$.get ("XMLFile.xml", function (XML) {
$ (XML). Find ("item"). length;
});
2. Reading XML content
If the read XML is coming from the XML file, this combination of the above points is handled as follows:
Copy Code code as follows:
$.get ("XMLFile.xml", function (XML) {
$ (XML). Find ("item"). length;
});
If you read an XML string, be aware that XML strings must be surrounded by "<xml>" and "</xml>" to be parsed
Copy Code code as follows:
$ ("<xml><root><item></item></root></xml>"). Find ("item"). length;
Parsing XML content:
Sample xml:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<fields>
<field name= "Name1" >
<fieldname>dsname</fieldname>
<datatype> characters </datatype>
</field>
<field name= "Name2" >
<fieldname>dstype</fieldname>
<datatype> characters </datatype>
</field>
</fields>
The following is the parse sample code:
Copy Code code as follows:
$ (XML). Find (' field '). each (function () {
var field = $ (this);
var fName = field.attr ("Name");//Read Node properties
var dataType = Field.find ("DataType"). Text ()//Read child node values
});