Load and parse XML data using AJAX in jQuery

Source: Internet
Author: User
Use AJAX in jQuery to load XML data and parse/******* 1. Content-Type

02 in many cases, the problem of Content-Type cannot be resolved.

03 if it is an xml file, skip this step.

04 to dynamically generate XML, you must set it to text/xml. Otherwise, text/html is the common text by default.

05 set the Content-Type in common languages *********/

06

07 header ("Content-Type: text/xml"); // php

08 response. ContentType = "text/xml" // asp

09 response. setHeader ("ContentType", "text/xml"); // jsp

10 /******

11 2. xml structure.

12 XML must be closed. It is very important!

13 examples:

14. XML *********/

15

16

17 Zhangsan

18 1

19 Lisi

20 2

21

22 // correct

23

24

25

26

27 Zhangsan

28 1

29

30

31 Lisi

32 2

33

34

35

36 /******

37 3, analysis

38 here macnie's

39 traverse student (here we still use the above XML and the sub-node is student )*********/

40

41

42 $. ajax ({

43 url: 'ajax. asp ',

44 type: 'get ',

45 dataType: 'xml', // do not write text or html here !!!

46 timeout: 1000,

47 error: function (xml ){

48 alert ('error loading XML document '+ xml );

49 },

50 success: function (xml ){

51 $ (xml). find ("student"). each (function (I ){

52 var id = $ (this). children ("id"); // retrieves an object

53 var idvalue = $ (this). children ("id"). text (); // get text

54 alert (id_value); // here is the ID value.

55 alert ($ (this). attr ("email"); // The email attributes under student are displayed here.

56 // the final result is output. This is the CSS rain method, which seems to be a little more JQ than macnie.

57 $ ('

  • ')

    58. html (id_value)

    59. appendTo ('ol ');

    60 });

    61}

    62 });

    63

    64 // Add the last line: ensure that the server side is UTF-8 encoded; otherwise, garbled characters will occur! Make sure that your xml file is in UTF-8 format.


    The above is the content for loading and parsing XML data using AJAX in jQuery. For more information, see PHP Chinese website (www.php1.cn )!

    Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.