Example 1: ========================================================== ================== XML content: <? XML version = "1.0" encoding = "UTF-8"?> <Stulist> <Man> <Name> zdz </Name> <Age> 25 </age> </Man> <Man> <Name> Liufeng </Name> <Age> 22 </age> </Man> <Man> <Name> hotman </Name> <Age> 18 </age> </Man> </Stulist> ============================================ <SCRIPT> $ (Document). Ready (function (){ $. Ajax ({URL: "test. xml", datatype: "XML", success: function (XML ){ $ (XML). Find ("stulist> Man"). Each (function (){ Document. getelementbyid ("zdzdiv "). innerhtml + = "<br> name:" + $ (this ). find ("name "). text () + "<br/> Age:" + $ (this ). find ("Age "). text (); }); }}); }) </SCRIPT> <Div id = "zdzdiv"> ============================================ Parameter 1 URL: Path of the document to be requested. Parameter 2 datatype: the requested document type. xml HTML Script JSON and so on. Parameter 3 success: the callback function after the request is successful. $ (XML). Find ("stulist> Man"). Each finds the man tag under the stulist node in the returned XML, and executes the function based on the search result. Search for child elements under the man tag and place the values in the displayed Div. ========================================================== ========================================================== ============ Example 2: Function resultxml () { $. Ajax ({ URL: 'ajaxrespose. aspx ', Data: {ID: 1, name: $ ('# gender'). Val ()}, Datatype: 'xml ', Success: function (XML) { Alert (XML );
$ (XML). Find ('select'). Each (function () { VaR option1 = "<option value = '"; VaR option2 = "'> "; VaR option3 = "</option> "; VaR text = $ (this). Children ("text"). Text (); VaR value = $ (this). Children ("value"). Text (); $ ("# City"). append (option1 + value + option2 + TEXT + option3 );
}); } }); } XML file returned by the server String targetid = request. querystring ["ID"]; // obtain the value of the parameter ID in the request. String xml_version = "<? XML version =/"1.0/" encoding =/"UTF-8/"?> "; String xml_start = "<selects> "; String xml_end = "</selects> "; String xml = ""; Switch (targetid) { Case "0 ": Xml = "<SELECT> <value> 0 </value> <text> select </text> </SELECT> "; Break; Case "1 ": Xml = "<SELECT> <value> 1 </value> <text> Changping </text> </SELECT> "; XML + = "<SELECT> <value> 2 </value> <text> Fengtai </text> </SELECT> "; Break; Case "2 ": Xml = "<SELECT> <value> 1 </value> <text> Tanggu District </text> </SELECT> "; XML + = "<SELECT> <value> 2 </value> <text> hangu </text> </SELECT> "; Break; } String last_xml = xml_version + xml_start + XML + xml_end; Response. contenttype = "text/XML"; // The returned data type is XML (required) Response. Write (last_xml ); Response. End (); |