处理xml代码时的InputStream异常

时间:2012-10-04 06:41:52

标签: blackberry io

我正在尝试tp解析存储在服务器中的黑莓中的xml。但它出现在输入流行中有不同的异常。主要是数据报协议和TcpInput例外。我已经附上我的代码,请指导我。

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());
}

2 个答案:

答案 0 :(得分:2)

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;

公共类CopyOfXMLParser扩展了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");

我使用此代码获取所有元素值     / * /

答案 1 :(得分:0)

使用Sax解析XML解析。 请参阅此链接

http://stackoverflow.com/questions/11901822/xml-parsing-using-sax-for-blackberry/11914662#11914662