Browse XML located in the console

0

I was making a request through AJAX and I found that I do not know how to handle the information that arrives afterwards. This request returns an XML (in the console) like the one below; which I can see by console or pass it to String format and put it on the web within a paragraph (but this last does not make it easier for me to search for labels precisely). Now, if you wanted to extract, for example, the value of the label   I mean, the "-1.98", what would be the best way to do it?

The idea is to collect the data of "person", "latitude" and "length" of each element of the XML.

<?xml version="1.0" encoding="UTF-8"?>
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
<head>
 <variable name="person"/>
 <variable name="personLabel"/>
 <variable name="image"/> 
 <variable name="deathPlace"/>
 <variable name="deathDate"/> 
 <variable name="coordinates"/> 
 <variable name="deathPlaceLabelEsp"/> 
 <variable name="latitud"/>
 <variable name="longitud"/> 
</head>
 <results> 
 <result> 
 <binding name="deathPlace">
 <uri>http://www.wikidata.org/entity/Q10313</uri>
</binding>
 <binding name="coordinates"> 
<uri>http://euskadi.eus/geo-point--198-4332</uri>
 </binding>
 <binding name="latitud">
 <literal datatype="http://www.w3.org/2001/XMLSchema#double">-1.98</literal> </binding>
 <binding name="longitud"> 
<literal datatype="http://www.w3.org/2001/XMLSchema#double">43.32</literal>
 </binding>
 <binding name="person">
 <uri>http://www.wikidata.org/entity/Q19312438</uri>
 </binding>
 <binding name="image"> <uri>http://commons.wikimedia.org/wiki/Special:FilePath/Nemesio%20Manuel%20Sobrevila%201928.jpg</uri>
 </binding>
 <binding name="deathDate">
 <literal datatype="http://www.w3.org/2001/XMLSchema#dateTime">1969-01-01T05:00:00.000Z</literal>
 </binding>
 <binding name="personLabel">
 <literal xml:lang="es">Nemesio Manuel Sobrevila</literal>
 </binding>
 <binding name="deathPlaceLabelEsp">
 <literal xml:lang="es">San Sebastián</literal>
 </binding>
 </result>
 <result>
 <binding name="deathPlace">
 <uri>http://www.wikidata.org/entity/Q11994</uri>
 </binding>
 <binding name="coordinates">
 <uri>http://euskadi.eus/geo-point--2933333333-42683333333</uri>
 </binding>
 <binding name="latitud">
 <literal datatype="http://www.w3.org/2001/XMLSchema#double">-2.933333333</literal> </binding>
 <binding name="longitud">
 <literal datatype="http://www.w3.org/2001/XMLSchema#double">42.683333333</literal> </binding>
 <binding name="person">
 <uri>http://www.wikidata.org/entity/Q5872386</uri>
 </binding>
 <binding name="image"> <uri>http://commons.wikimedia.org/wiki/Special:FilePath/Felix%20Padin%20Albertian.JPG</uri>
 </binding>
 <binding name="deathDate">
 <literal datatype="http://www.w3.org/2001/XMLSchema#dateTime">2014-10-07T04:00:00.000Z</literal>
 </binding>
 <binding name="personLabel"> 
<literal xml:lang="es">Félix Padín</literal>
 </binding>
 <binding name="deathPlaceLabelEsp">
 <literal xml:lang="es">Miranda de Ebro</literal>
 </binding> 
</result> 

This is the code by which I send the request to the database:

var options = {
        "async": true,
        "crossDomain": true,
        "url": "...any URL...",
        "method": "POST",
        "dataType":"xml",
        "headers": {
          "Content-Type": "application/x-www-form-urlencoded",
          "Accept" : "application/sparql-results+xml;charset=UTF-8",
          "Cache-Control": "true",        
        },

        "data": 
            "query=PREFIX wd: <http://www.wikidata.org/entity/>" +
            "PREFIX wdt: <http://www.wikidata.org/prop/direct/>"+
            "PREFIX geo: <http://www.opengis.net/ont/geosparql#>"+
            "SELECT *"+
            "WHERE {"+
            " ?person rdfs:label ?personLabel ."+
            " ?person wdt:P19 wd:Q8692 ."+
            " ?person wdt:P18 ?image ."+
            " ?person wdt:P20 ?deathPlace ."+
            " ?person wdt:P570 ?deathDate ."+
            " ?deathPlace wdt:P625 ?coordinates ;"+
            " rdfs:label ?deathPlaceLabelEsp."+
            " ?coordinates geo:lat ?latitud;"+
            " geo:long ?longitud."+
            "}"


      }


      $.ajax(options).done(function (respuesta) {
        console.log(respuesta);
}

That last console.log is that he returns the XML to me by console, but I do not know how to access the data to work with them.

Thanks in advance!

    
asked by Jonan87 04.01.2018 в 17:16
source

1 answer

2

If it comes to you as% well-formed% co, with XML you should be able to read the tree just like you read the DOM.

$(respuesta).find("results").find("result").each(function(index,element){
  console.log($(element).find("binding[name='person']").find("uri").text());
  console.log($(element).find("binding[name='latitud']").find("literal").text());
  console.log($(element).find("binding[name='longitud']").find("literal").text());
});

I understand that this should show you the data you're looking for.

In the case that you got it as plain text and not as jQuery you should pause it:

var xamlParsed=$.parseXML(respueta);

And you could read it as I described above.

    
answered by 04.01.2018 / 17:43
source