First scenario:
The code is as follows |
Copy Code |
<script type= "Text/javascript" > $ (document). Ready (function () { $.ajax ({ URL: '/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> </ol> </div> |
The second option:
The code is as follows |
Copy Code |
<script type= "Text/javascript" > $.get ("/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:
The code is as follows |
Copy Code |
$.get ("XMLFile.xml", function (XML) { XML is the content that can be read and read in detail, see 2nd }); |
2. Reading XML content
If the read XML is coming from the XML file, this combination of the above points is handled as follows:
The code is as follows |
Copy Code |
$.get ("XMLFile.xml", function (XML) { XML is the content that can be read and read in detail, see 2nd }); |
If you read an XML string, be aware that XML strings must be surrounded by "<xml>" and "</xml>" to be parsed
The code is as follows |
Copy Code |
$ ("<xml><root><item></item></root></xml>"). Find ("item"). length; |
Parsing XML content:
Sample xml:
The code is as follows |
Copy Code |
<?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:
The code is as follows |
Copy Code |
$ (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 }); |
Let's look at a simple example
The code is as follows |
Copy Code |
XMLFile.xml <?xml version= "1.0" encoding= "Utf-8"?> <msglist> <msg name= "One" > <id>1</id> <content>content1</content> </msg> <msg name= ">" <id>2</id> <content>content2</content> </msg> </msglist>
Jqxmlfirst.aspx <%@ Page language= "C #" autoeventwireup= "true" codefile= "JqXmlFirst.aspx.cs" inherits= "Jqueryxml_jqxmlfirst"%> <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> Introduction to <title>jquery Operations XML </title> <script type= "Text/javascript" src= ". /js/jquery-1.2.6.js "></script> <script type= "Text/javascript" > $ (function () { $.ajax ({ URL: "XMLFile.xml", DataType: "xml", Error:function (XML) { Alert (' Error loading XML document ' +xml); }, Success:function (XML) { $ (XML). Find ("Msglist > Msg"). each (function () { Alert ($ (this). Find ("Content"). text ();//each is a loop execution, that is, multiple pop-ups. Alert ($ (this). attr ("name");//Method of getting properties }); } }) }); </script> <body> <form id= "Form1" runat= "Server" > <div> </div> </form> </body>
|