1

I'm trying to extract data for the hotel names at the given latitude and longitude in JAVA. I'm getting the following error: [Fatal Error] :1:1: Content is not allowed in prolog. Here is the code and the URL from where I am trying to extract my information. Any suggestion about the issue?

              URL url = new URL("https://api.eancdn.com/ean-services/rs/hotel/v3/list?apiKey=vkndmgahz5aekd65pxg4rvvp&locale=en_US&currencyCode=USD&latitude=51.514&longitude=-0.269"");
              InputStream is = url.openStream();
              DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
              DocumentBuilder db = dbf.newDocumentBuilder();
              Document doc = db.parse(is);

                NodeList itemList = 
                        doc.getElementsByTagName("HotelSummary");
                Node itemNode;
                Element itemElt;


                for(int k=0; k < itemList.getLength(); k++)
                {
                    itemNode = itemList.item(k);

                    if(itemNode.getNodeType() == Node.ELEMENT_NODE) {
                        itemElt = (Element) itemNode;
                        System.out.println("Hotel name: "+itemElt.getElementsByTagName("name").item(0).getTextContent());
chelayel
  • 47
  • 5
  • possible duplicate of [org.xml.sax.SAXParseException: Content is not allowed in prolog](http://stackoverflow.com/questions/5138696/org-xml-sax-saxparseexception-content-is-not-allowed-in-prolog) – Alan Oct 24 '14 at 15:53
  • I've checked the other post but it did no solve my issue since there is no UTF-8 in the beginning of the xml file in addition to the file being retrieved from an online data base – chelayel Oct 24 '14 at 16:17

1 Answers1

1

What's Going Wrong

If you run the following code, you will see the data you are getting back is not XML, but JSON.

import java.net.URL;
import java.io.InputStream;

public class Demo {

    public static void main(String[] args) throws Exception {
        URL url = new URL("https://api.eancdn.com/ean-services/rs/hotel/v3/list?apiKey=vkndmgahz5aekd65pxg4rvvp&locale=en_US&currencyCode=USD&latitude=51.514&longitude=-0.269");
        InputStream is = url.openStream();

        int next = is.read();
        while(next != -1) {
            System.out.print((char) next);
            next = is.read();
        }
    }

}

Getting the Data as XML

You can use an HttpURLConnection to request the data as XML:

import java.net.HttpURLConnection;
import java.net.URL;
import java.io.InputStream;
import javax.xml.parsers.*;
import org.w3c.dom.Document;

public class Demo {

    public static void main(String[] args) throws Exception {
        URL url = new URL("https://api.eancdn.com/ean-services/rs/hotel/v3/list?apiKey=vkndmgahz5aekd65pxg4rvvp&locale=en_US&currencyCode=USD&latitude=51.514&longitude=-0.269");

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept", "application/xml");
        InputStream is = connection.getInputStream();

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(is);
    }

}
bdoughan
  • 147,609
  • 23
  • 300
  • 400