jQuery Ajax call and result XML parsing

By Chris Gaskell

I have found jQuery to be one of the most powerful JavaScript libraries out there at the minute. I’ve been working with the base library and a number of plug-ins over the past couple of weeks and have found both the effects and AJAX methods to be both powerful and easy.

Here’s some code that I have used to look up an address. The data is served by an .aspx (.NET) page and I have included an example of the returned XML below.

jQuery code:

//Perform Ajax call – notice the number and postcode – these end up as querystring params and are taken in by AddressFinder.aspx
//’xml’ is an XML document object holding the returned XML
jQ.ajax({ type: “POST”, url: “AddressFinder.aspx”, data: “number=1&postcode=wn58ln”, dataType: “xml”, success: function(xml){

//This simple XPath XML function looks through the returned XML data
//All tags can now be accessed within the loop
var _number = “”;
var _line1 = “”;
var _town = “”;
var _city = “”;
var _country = “”;

//This function will loop for each match on addresses/address
jQ(“/addresses/address”, xml).each(function(){

_number = jQ(“number”, this).text();
_line1 = jQ(“line1″, this).text();
_town = jQ(“town”, this).text();
_city = jQ(“city”, this).text();
_country = jQ(“country”, this).text();

jQ(“#text11″).val(_line1);
jQ(“#text12″).val(_town);
jQ(“#text13″).val(_city);
jQ(“#text14″).val(_country);
});

XML returned from AddressFinder.aspx:

<?xml version=”1.0″ encoding=”utf-8″ ?>
<addresses>
<address>
<number>6</number>
<line1>Almsberry Cresent</line1>
<town>Ashton – in – Makerfield</town>
<city>Wigan</city>
<country>England</country>
</address>
</addresses>

Did you find this demonstration helpful? If so leave me a comment and let me know if there are any other AJAX exmaples you’d like to see here. Thanks.

92 Responses to “jQuery Ajax call and result XML parsing”

  1. Lars Says:

    I know that it might seem obvious, but it is very important to remember to set the text/xml content type on the http headers.

    One thing that I do miss is the ability to select properties in a xml document like tada. How would you go about selecting the number in this case?

  2. Chris Gaskell Says:

    Lars,
    I’m not totall sure what you mean here. why not send me an email on chris_gaskell at blueyonder dot co dot uk.

    Thanks,
    Chris

  3. muki Says:

    Hi,
    I want to parse the soap respone . How I can do this using Xpath ?

    Muki

  4. Kanchana Says:

    Hi,

    How can i get the node names in jquery?.because nodeName is not working here.is there any Jquery statement so that i can print all node Names.

    Thanks
    Kanchana

  5. Rasmus Says:

    This was very helpful.
    Thanks alot Chris!

  6. Bilange Says:

    Lars, thank you for stating the obvious about sending the good content-type back.

    You just ended my 3 hours of insane debugging, test-casing, head-banging… you name it :)

    All that because i forgot to specify “text/xml” im my PHP file :S

    So long story short for everyone else google’ing around: if IE fails at XML parsing but Firefox is okay, be sure to add this line in your php file:

    header(‘Content-type: text/xml’);

    Javascript will then treat this input as a valid XML (and IE wont crash on it!)

    Many thanks :)

  7. ndnookala Says:

    Chris,
    this kinda example on XML parsing is not even mentioned in JQuery site tutorials. It is really helpful for me.

    Thank you very much :)

  8. Caustic Dave Says:

    Thank you for the clear example. I need to try it out ASAP for a current project.

    Please write more of these posts!

  9. Miro Says:

    Nice example ! Thanks

  10. hatem Says:

    Thanks Bilange, you saved me a lots of time :)

  11. 240 plugins jquery : sastgroup.com Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  12. Propiedad Privada » Blog Archive » 240 Plugins para jQuery Says:

    [...] Plugin jQuery Ajax call and result XML parsing xmlObjectifier – Converts XML DOM to JSON jQuery XSL Transform jQuery Taconite – multiple Dom [...]

  13. Brian Says:

    I’m having a straight xml load fail on one of my companies servers, but not another server and only in IE, unfortunitly, the server it fails on is the one I need for production. IE works on the other non-production server. Any ideas? Can you do just a straight xml file load like this?

    $.ajax({
    type: “Get”,
    url: “ambassadors.xml”,
    dataType: “xml”,

  14. chinatian › jQuery插件超级多 Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  15. Charlie Says:

    Thank you very much for your comments everyone. It helped me a lot to debug one of my web applications.

  16. Diversos Links para desenvoledores de javascript | Blog do teo Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  17. Ash Says:

    Code markup is awful. Can You fix it, please? Completely unreadable.

  18. johnmazarr Says:

    A Realistic $250K First Year Income Potential

    Less than four years ago I was driving this beat-up ‘94 VW with a rusty muffler. Within two years of creating this system I was making more than my doctor, accountant, and attorney COMBINED… while working less than 40 hours a week FROM HOME! Together with my group of leaders we are now seeking qualified entrepeneurs sharein the incredible results of our system.

    for more information check this site out….
    Reverse Funnel System

  19. Martin Sarsini Says:

    I changed it slightly

    response = $(‘feedback’, this).text();

    Thank you very much! Really helpful

  20. jay bharat Says:

    good help

  21. anibal Says:

    Great! this is an example that I need and couldn’t find in official jQuery’s site.
    Thank you

  22. The ultimate jQuery Plugin List | Kollermedia.at Says:

    [...] Plugin jQuery Ajax call and result XML parsing xmlObjectifier – Converts XML DOM to JSON jQuery XSL Transform jQuery Taconite – multiple Dom [...]

  23. 240 adet jquery ekelntisi - Volkan Şentürk Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  24. Sam Says:

    Hi All,

    I’m an author of jQuery plugin(s) that specifically handle cases of working with SOAP web services and other XML responses. So if you’re really in need of a solution please visit my site http://www.terracoder.com. Link to it is also available from jQuery site. Enjoy!!!

    • Fernando Says:

      Sam,

      I’m really interested in jsXMLUtils JQuery plugin, but your site http://www.terracoder.com it’s not working since several days due to site maintenance,..

      Do I have to go to any other url to reach docs, samples, etc ?

      Thanks in advance !!!

  25. » 1000 ressources pour le développement web et WordPress : la grosse grosse liste « css4design : des css pour votre design html Says:

    [...] jQuery Ajax call and result XML parsing [...]

  26. 240 plugins jquery | Computer and Technoloy News Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  27. 翟鹏的博客 » 240个JQuery插件 Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  28. h4wk Says:

    thank you. its a very usefull example.

  29. Hidden Pixels - JQuery Examples Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  30. jQueryTips » รวมฮิต jQuery Plugins มากกว่า 200 รายการ Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  31. Paul Says:

    Please note that XPath support was removed in jQuery 1.2, so the above example does not work anymore in its current form.

    replace

    /addresses/address

    with the css selector

    >addresses>address

  32. more information here Says:

    more information here…

    Nice Content Very Usseful…

  33. 200+ jQuery插件 | 浮世失焦 Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  34. 多个jquery效果演示效果示例 - 虾米碗糕|十里地后院 Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  35. ryan Says:

    nice tutorial!! thanks!!

  36. 51 Ajax scripts for .net « The Adventures of Amit Dua Says:

    [...] jQuery xml parsing [...]

  37. jQuery Eklentileri | Bir Öğrenci Klasiği Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  38. 240 удивительных плагинов для jQuery | Parinoff Life Says:

    [...] jQuery Ajax call and result XML parsing [...]

  39. nafis Says:

    nice works, it a great help …

  40. Lazystudio Blog » Blog Archive » 荐:240多个jQuery插件 Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  41. I’m Chain » jquery » JQuery Plugin 2008 Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  42. Excelente listado de 240 plugins para jquery | Adictos a la red Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  43. 240 plugin hữu ích của JQuery « Anhcaxomlieu’s Blog - Phạm Ngọc Hùng BKA Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  44. Virtex Says:

    Очень признателен, действительно полезная инфа.

  45. jQuery好用的240 plugin list | 偉士牌的實驗室 Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  46. Neil’s Blog » Blog Archive » The ultimate jQuery Plugin List Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  47. birkof’s blog » Blog Archive » Lista de plugin-uri jQuery Says:

    [...] Plugin jQuery Ajax call and result XML parsing xmlObjectifier – Converts XML DOM to JSON jQuery XSL Transform jQuery Taconite – multiple Dom [...]

  48. 240+ plugins para jQuery | Lo Pongo Acá Says:

    [...] Plugin jQuery Ajax call and result XML parsing xmlObjectifier – Converts XML DOM to JSON jQuery XSL Transform jQuery Taconite – multiple Dom [...]

  49. 240 plugins para JQuery | Marcelo Vega Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  50. Mais de 200 Plugins utilizando Jquery « Jorge Eduardo Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  51. » 240 plugins JQuery Darío Ferrer Says:

    [...] jQuery Ajax call and result XML parsing [...]

  52. VietNam PHP Blog » Blog Archive » 240 plugin hữu ích của JQuery Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  53. DOUAMI » Plugins pour jQuery Says:

    [...] jQuery Ajax call and result XML parsing [...]

  54. 100+ jquery插件应用,让你的Web开发事半功倍 - 菠菜博 Says:

    [...] Plugin jQuery Ajax call and result XML parsing xmlObjectifier – Converts XML DOM to JSON jQuery XSL Transform jQuery Taconite – multiple Dom [...]

  55. 100+ jquery插件应用,让你的Web开发事半功倍 | 高手网 Says:

    [...] jQuery Ajax call and result XML parsing [...]

  56. WANTED Says:

    Автор, а скажите а куда написать по поводу обмена ссылок (на какое конкретно мыло)?

  57. 200+ jQuery插件 | forcto.com Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  58. List of categorised Jquery Plugins | Expertz Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  59. 我想网 » Blog Archive » 240多个jQuery插件 Says:

    [...] Plugin . jQuery Ajax call and result XML parsing . xmlObjectifier – Converts XML DOM to JSON . jQuery XSL Transform . jQuery Taconite – multiple Dom [...]

  60. Сергей Says:

    Эх… После прочтения даже мне эта тема стала интересна.

  61. yr Says:

    te

  62. Lista plugins jquery | Geekblog Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  63. Auto Says:

    Hello…

    Gotta love google, very neat stuff. Thanks….

  64. 强烈推荐:240多个jQuery插件 « 万里船 Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  65. Leigh Says:

    This is most helpful. Could you put up what the .net program looks like. I have interfaced with a php pgm but do not understand the mechanics of a .net.

  66. 240 plugin hữu ích của JQuery « Thông tin – Quảng cáo – Giới thiệu Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  67. Sangam Uprety Says:

    Very helpful. I just retrieved the xml data from my webservice using jquery ajax. Now I will be manipulating the xml received.

    Thank you.

  68. JQuery: Пара сотен плагинов | peopleofdesign.net | Современник Says:

    [...] Plugin jQuery Ajax call and result XML parsing xmlObjectifier – Converts XML DOM to JSON jQuery XSL Transform jQuery Taconite – multiple Dom [...]

  69. Marc Says:

    Thanks for your post! I’m sure all of us appreciate the fact that you’ve taken the time to take your findings, big or small, and share them with the rest of our web developing community!

  70. Lindsay Says:

    Thank you very much Lars and Bilange about your comments about including the right xml header. I wish I had read this before I wasted 2-3 hours trying to debug my own application.

  71. 240 de pluginuri jquery | javascript&jquery Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – [...]

  72. 240 plugin hữu ích của JQuery - Cộng đồng lập trình viên VIỆT Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  73. 240+ awesome Plugins in the List « Er.Krushna Chandra Muni :: Website Design Orissa,Website Design Bhubaneswar,Indian Web Designer,Indian Freelancer,website design india Says:

    [...] Plugin jQuery Ajax call and result XML parsing xmlObjectifier – Converts XML DOM to JSON jQuery XSL Transform jQuery Taconite – multiple Dom [...]

  74. 240 plugin hữu ích của JQuery « Nothing like Impossible – Đỗ Mạnh Hùng Says:

    [...] [...]

  75. 240 plugin hữu ích của JQuery Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  76. 240 cool jQuery plugins Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  77. 240多个jQuery插件 | Shepgala Design Says:

    [...] Plugin.jQuery Ajax call and result XML parsing.xmlObjectifier – Converts XML DOM to JSON.jQuery XSL Transform.jQuery Taconite – multiple Dom [...]

  78. 强烈推荐:240多个jQuery插件 - Hobo Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – [...]

  79. 200 jQuery plugins | Sinoyan Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  80. jQuery插件下载地址大全 Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  81. 240 plugins jquery - AdaptBlog Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple Dom [...]

  82. jQuery Eklentileri 1 « Webmaster Destek Cms Seo Makaleleri Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – [...]

  83. 240多个jQuery插件 - Jidda's Blog Says:

    [...] jQuery Portlets. jqDnR – drag, drop resize. Drag Demos. XML XSL JSON FeedsXSLT Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – [...]

  84. Free Style Live » Blog Archive » 强烈推荐:240多个jQuery插件 Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – [...]

  85. 240 plugins Jquery | flazann Says:

    [...] Plugin jQuery Ajax call and result XML parsing xmlObjectifier – Converts XML DOM to JSON jQuery XSL Transform jQuery Taconite – multiple Dom [...]

  86. List of jQuery plugins » IMvsMI blog Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – multiple [...]

  87. JQuery: Пара сотен плагинов в одной заметке @ Тестовый блог Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – [...]

  88. 240多个jQuery插件下载地址 Says:

    [...] Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – [...]

  89. 01WEB-专注于 网页设计 网站开发 用户体验 收集创意 采集灵感 发现喜悦 纯净生活 » Blog Archive » 两百个jQuery插件集合 Says:

    [...] XSL JSON Feeds XSLT Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – [...]

  90. 使用jquery的秘籍-juery插件大本营 - 闪奕Flash培训博客———我爱Flash Says:

    [...] jQuery Ajax call and result XML parsing. [...]

  91. He Min Blog» Blog Archive » 各种jQuery插件 Says:

    [...] XSL JSON Feeds XSLT Plugin. jQuery Ajax call and result XML parsing. xmlObjectifier – Converts XML DOM to JSON. jQuery XSL Transform. jQuery Taconite – [...]

Leave a Reply