How to scraping a remote xml file?

3

I want to get certain data from an open-access XML file on a remote server.

These are Open Data files of AEMET. The idea is to read the xml and extract some information. I want to do it using javascript, so that I can visualize in a web this data obtained from the xml file. As an example one of the files is;

link

I understand how to do it in Python; I create a variable with the content of the XML and then with BeautifulSoup or any other scrapper I read the DOM data from the xml. I have no idea how to start javascritp. How do I read the remote xml? from there I think I understand how I can get the data for each label.

I have looked how to do it and everything I find seems oriented to Ajax, but I did not find how to do it by reading directly the remote xml.

    
asked by kamome 23.03.2016 в 17:29
source

1 answer

3

You can specify the dataType of the $ .ajax function of jQuery as "xml"

Example

$("button").click(function(){
     
    $.get({
        url: "http://www.aemet.es/xml/maritima/FQXX46MM.xml",
        dataType: "xml"
    })
    .done(function(data) {
        // Has algo con la variable data
        alert(data);
    })
    .error(function(e){
        //Desafortunadamente el recurso XML al estar en otro dominio no permite ser obtenido por AJAX al no tener el header: "Access-Control-Allow-Origin"
        alert(e.statusText);
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button>Prueba ajax con xml</button>

Unfortunately, as the comment indicates, the particular resource you are trying to access: link does not deliver in the response the header "Access-Control-Allow-Origin" so the browser will block the request showing the following message (in Chrome):

XMLHttpRequest cannot load http://www.aemet.es/xml/maritima/FQXX46MM.xml. 
No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'null' is therefore not allowed access.

So, at least that particular file can not be read from JavaScript, unless that page lists the domain where your script is hosted as allowed.

    
answered by 24.03.2016 / 07:48
source