tags:

views:

49

answers:

1

Hello, I'm trying to parse this file on Android using the DOM method.

The code in question is:

try {
    URL url = new URL("https://www.beatport.com/en-US/xml/content/home/detail/1/welcome_to_beatport");

    InputSource is = new InputSource(url.openStream());

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(is);
    document.getDocumentElement().normalize();
} catch(Exception e) {
    Log.v(TAG, "Exception = " + e);
}

But I'm getting the following exception:

V/XMLParseTest1(  846):Exception = org.xml.sax.SAXParseException: name expected (position:START_TAG <null>@2:176 in java.io.InputStreamReader@43ea4538) 

The file is being handed to me gzipped. I've checked the is object in the debugger and its length is 6733 bytes (the same as the content length of the file in the response headers) however if I save the file to my harddrive it's size is 59114 bytes. Furthermore if I upload it to my own server which doesn't gzip XML-s when it serves them and set the URL the code runs just fine.

I'm guessing that what happens is that Android tries to parse the gzipped stream.

Is there a way to first unzip the stream? Any other ideas?

+2  A: 

You can wrap the result of url.openStream() in a GZIPInputStream. eg:

InputSource is = new InputSource(new GZIPInputStream(url.openStream()));

To auto-detect when to do this, use the Content-Encoding HTTP header. eg:

URLConnection connection = url.openConnection();
InputStream stream = connection.getInputStream();
if ("gzip".equals(connection.getContentEncoding())) {
  stream = new GZIPInputStream(stream));
}
InputSource is = new InputSource(stream);
Laurence Gonsalves
Thanks a lot. One more question: is there a way to find out if a stream is gzipped?
kitsched
Also thanks for your edit on the auto-detection issue.
kitsched