1

I am trying to access the body of an external HTML page with jquery. The body of that page contains an XML structure, which I would like to transform to JSON for further data accessing.

$.ajax({        
           url:'http://pruebanico.comze.com/testxml.html',  
           dataType:'html',
           type:'POST',
           timeout:20000,  
           success:function(response, status) {         

              var xmlDoc = $.parseXML(response);
              xmlBody = xmlDoc.find('body');
              alert(xmlBody);
              var jsonString = xml2json(xmlBody, '', html);
              alert(jsonString);

 }

I am using the following xml2json converter: http://www.thomasfrank.se/xml_to_json.html.

The first alert (xmlBody) is empty. I wanted to parse the text content of the body to a valid XML structure as input for the xml2json function, but without much success.

nicBBB
  • 267
  • 1
  • 4
  • 18
  • Are you performing the ajax request from a different domain to http://pruebanico.comze.com? If 'yes', then it won't work due to security restrictions. More about this here: http://stackoverflow.com/questions/7638773/cross-domain-requests-with-jquery – T. Junghans Mar 20 '12 at 10:15
  • No domain problem. I am overriding the domain restrictions via phonegap on mobile. – nicBBB Mar 20 '12 at 10:53

1 Answers1

1

You have a typo because you're using xmlDoc instead of xml (and by the way this is the body of the page the script is running in)

     var xml = $('body');

     var jsonString = xml2json(xmlDoc, '', html);

and remember that you can't make a call to:

 url:'http://pruebanico.comze.com/testxml.html',  

unless your script runs on the same domain

EDIT - you could try

var xmlDoc = $.parseXML(xml);
xmlBody = $(xmlDoc).find('body');
sinemetu1
  • 1,726
  • 1
  • 13
  • 24
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
  • No domain problem as using phonegap with jquery mobile. As the body selector returns an object and the xml2json needs a string, how can I convert the body object to a string? I need somehow an additional step that converts the body object to an xmlDoc string. – nicBBB Mar 20 '12 at 11:01
  • @nicBBB if it needs a string you should use $('body').html() that returns the html (it's an xml since html is xml) that's inside the body tag – Nicola Peluchetti Mar 20 '12 at 11:53
  • adding .html to the body selector indeed returns the html, but as you pointed out earlier its the html of the page the script is running in. How to obtain the body of the response of the ajax call? – nicBBB Mar 20 '12 at 12:19
  • @nicBBB i think that you should use response: you could use parseXML() on it and then use find(). I updated my answe – Nicola Peluchetti Mar 20 '12 at 12:24
  • Thanks for your effort Nicola. I updated my question with your comments, but still not working. The first alert is empty. – nicBBB Mar 20 '12 at 13:33
  • @nicBBB ok you need to wrap it into jQuery, then you should have object object as an alert – Nicola Peluchetti Mar 20 '12 at 13:49