Return xml string - how to parse an XML file using jQuery / Json

when I warn that it returns the string as follows:

data    "<?xml version="1.0" encoding="utf-8" ?> 
      <xml xmlns="http://www.opengis.net/kml/2.2">
      <Document>
      <Name>John Smith</Name> 
      <Description>stackoverflow</Description> 
      <Total>50</Total> 
      </Document>
      </xml>"

Update: I tried to use this getJSON method and I get warnings but never execute insidefind('Document').each.....

 $.getJSON(_url, function (data) {

            alert(data);    
            $(data).find('Document').each(function () {
                debugger
                var name = $(this).find('Name');
                var desc = $(this).find('Description').text();
                var total = $(this).find('Total').text()

            });

        });

how to read the xml file in jquery, below is what returns me as a string, and I see that when I do a warning (data);

 $.getJSON(url, {},
                function (data) {
                    alert(data);
             }
});


<?xml version="1.0" encoding="utf-8" ?> 
- <xml xmlns="http://www.opengis.net/kml/2.2">
- <Document>
  <Name>John Smith</Name> 
  <Description>stackoverflow</Description> 
  <Total>50</Total> 
  </Document>
  </xml>
+3
source share
4 answers

You seem to have misunderstood what JSON is and how it is used in jQuery !?

-, JSON. jQuery JSON, . "jsonp1291171891383 ({})", JavaScript. XML, JavaScript.

- - - "jsonp1 ({" data ":" <xml> "})". , "" , XML, .

.

jQuery.fromXMLString = function(strXML){
    if (window.DOMParser) {
        return jQuery(new DOMParser().parseFromString(strXML, "text/xml"));
    } else if (window.ActiveXObject) {
        var doc = new ActiveXObject("Microsoft.XMLDOM");
        doc.async = "false";
        doc.loadXML(strXML);
        return jQuery(doc);
    } else {
        return jQuery(strXML);
    }
};

:

 $.fromXMLString(data).find('Document').each( ... );
+1

, Google ajax API xml- > json.

http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q= url .

JSONP , :

var googleAPI = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&q=";

$.getJSON(googleAPI + url + "&callback=?", null, function(data) {

        alert(data);    
        $(data).find('Document').each(function () {
            debugger
            var name = $(this).find('Name');
            var desc = $(this).find('Description').text();
            var total = $(this).find('Total').text()

        });
});

JSON, Name, Description, Total . , JSON jQuery

+5

.getJSON XML-. :

$.ajax({
    url: url,
    data: {},
    success: function(data){
        // now you can traverse your data just like the DOM
        // e.g. 
        // alert( $(data).find('Document:first Name').text() );
    },
    dataType: 'xml'
});
0

, JSON, , , , XML , HTML, jQuery. $('Name') <Name> .. , data, - :

var people = data.children('Name');
0
source

Source: https://habr.com/ru/post/1777791/


All Articles