Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 111
Registered: ‎10-01-2009
My Device: Not Specified
Accepted Solution

XML parse gzip

I've seen posts on issues with retrieving XML and retrieving gzip data. Seperately, i have no problem doing either. But i am unable to send a gzipinputstream to my xml parser  - it just throws exception and says no stack trace.

I send a parameter in my http request that determines if the server should return an XML file or the gziped XML file.  when NOT requesting gzip, i can read and parse fine. if i request gzip and just read steam like this ... 

 

 

                byte[] data = new byte[256];
                int len = 0;
                int size = 0;
                StringBuffer raw = new StringBuffer();
                
                while ( -1 != (len = inputStream.read(data)) )
                {
                    raw.append(new String(data, 0, len));
                    size += len;
                }

it prints out just like my xml ... so i would assume it means its able to recieve the gzip data fine.

 

However, if i request gzip and try to parse using the same code that works when i just parse an xml, it fails.  Is there another step i need to do to convert the inputstream so the xml handler can read it?

 

 

httpConnection = (HttpConnection)Connector.open(url);
inputStream = httpConnection.openDataInputStream();
			
if(httpConnection.getResponseCode() == HttpConnection.HTTP_OK)
{

	inputStream = new GZIPInputStream(inputStream);
	InputSource is = new InputSource(inputStream);
	
	is.setEncoding(desiredEncoding);

	SAXParserFactory factory = SAXParserFactory.newInstance();
	SAXParser parser = factory.newSAXParser();

	DefaultHandler hcHandler = new XMLParse();

	parser.parse(is,hcHandler);

} //end if(httpConnection.getResponseCode() == HttpConnection.HTTP_OK)

 

 

 

 

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: XML parse gzip

 you should check to see if the content is gzip:

 

                String encoding = m_httpConnection.getHeaderField("Content-Encoding");
                if ((encoding != null) && (encoding.indexOf("gzip") != -1)){
                    compressed = true;
                }

 

Then, unzip it:

 

            if (compressed){
                GZIPInputStream gzip = new GZIPInputStream(dataInputStream);
                DataInputStream tmp = new DataInputStream(gzip);
                dataInputStream = tmp;
            }

Now you can safely parse the XML.

 

Developer
Posts: 111
Registered: ‎10-01-2009
My Device: Not Specified

Re: XML parse gzip

yup this works, thanks. unfortunately it appears gzip doesnt seem to solve my problem ... it appears it may be my xml parser thats taking a long time and not the http transfer. or more likely im just doing somethig else wrong.

New Developer
Posts: 6
Registered: ‎11-04-2009
My Device: Not Specified

Re: XML parse gzip

how to parse an xml in BB i m wondering to do xml parser in strom device

Highlighted
New Developer
Posts: 6
Registered: ‎11-04-2009
My Device: Not Specified

Re: XML parse

can anyone help me to do xml parser using BB rim api's

Developer
Posts: 111
Registered: ‎10-01-2009
My Device: Not Specified

Re: XML parse

@bala_eventurers: have you looked at the xmldemo?  i just used that as a template for mine.