Search code examples
blackberryio

InputStream exceptions while processing xml-code


hi am trying tp parse the xml in blackberry that stores in server. But it comes out in inputstream line with different exceptions.mostly with Datagram Protocol and TcpInput exceptions.i have attached my code here kindly guide me.

try {
    Document doc;
    StreamConnection conn = null;
    InputStream is = null;
    conn = (StreamConnection) Connector.open("http://xyz.com/GetImageDetails.xml;deviceside=true;");

    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setIgnoringElementContentWhitespace(true);
    docBuilderFactory.setCoalescing(true);

    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    docBuilder.isValidating();
    is = conn.openInputStream();
    doc = docBuilder.parse(is);
    doc.getDocumentElement().normalize();

    NodeList list = doc.getElementsByTagName("IMG_URL");
    for (int i = 0; i < list.getLength(); i++) {
        Node textNode = list.item(i).getFirstChild();
        System.out.println(textNode);
    }
} catch (Exception e) {
    System.out.println(e.toString());
}

Solution

  • The XML parsing class is
    /***/
    

    package com.application.xmlParser;

    import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.Hashtable; import java.util.Vector;

    import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.XMLReaderFactory;

    import com.application.log.Log;

    public class CopyOfXMLParser extends DefaultHandler {

    private String RecordElement;
    private String xmlURL;
    private Hashtable newObj = new Hashtable();
    private Vector Records = new Vector();
    private CallBack callBack ;
    private String _localEndTag = "";
    
    public void ParseInputStream(String stream,String rootElement, String recordElement , CallBack callBack) 
    {
        RecordElement = recordElement;
        this.callBack = callBack;
    
        InputStream in = new ByteArrayInputStream(stream.getBytes());
        try 
        {
            XMLReader reader = XMLReaderFactory.createXMLReader();
            reader.setContentHandler(this);
            reader.parse(new InputSource(in));
        } 
        catch ( Exception e ) 
        {
            System.out.println("#### ##### Parse Exception : " + e + "#### #####" + xmlURL);
          /*  callbackAdapter.callback(Records);*/
        }
    }
    
    
    public void startElement(String Uri, String localName, String qName, Attributes attributes) throws SAXException 
    {
    }
    
    public void characters(char [ ] ch, int start, int length) throws SAXException 
    {
       String elementValue = new String(ch, start, length).trim();
    
        Log.d("End Tag", _localEndTag); 
        Log.d("Tag Value ", elementValue);  
    
        if(_localEndTag ==  null || _localEndTag.equalsIgnoreCase(""))
        {
    
        }
        else
        {
            newObj.put((String)_localEndTag, (String)elementValue);
        }
    }
    
    public void endElement(String Uri, String localName, String qName) throws SAXException 
    {
        _localEndTag = localName;
    
        if ( localName.equalsIgnoreCase(RecordElement) ) 
        {
            Records.addElement(newObj);
            callBack.callBack(Records);
            System.out.println("###### ###### FINISH ###### ######" +RecordElement);
        }
    }
    

    }

    /***/
    
    /***/
    How to Implement
    
    take the (Http)InputStream response into String  and call the below method ..your problem is solved 
    
    new CopyOfXMLParser().ParseInputStream(new String(baos.toByteArray()) ,"SOAP:Envelope", "SOAP:Envelope");
    

    I am getting all the elements value , with this code /*/