blackberry - 处理 xml 代码时的输入流异常

标签 blackberry io

您好,我正在尝试解析存储在服务器中的黑莓中的 xml。但它在输入流行中出现,但有不同的异常。主要是数据报协议(protocol)和 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());
}

最佳答案

The XML parsing class is
/***/

包com.application.xmlParser;

导入java.io.ByteArrayInputStream; 导入 java.io.InputStream; 导入java.util.Hashtable; 导入java.util.Vector;

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

导入com.application.log.Log;

公共(public)类 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");

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

关于blackberry - 处理 xml 代码时的输入流异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12721655/

相关文章:

iphone - 最小的音频文件格式是什么?

c++ - 在 C++ 中使用字符指针将字符串保存到类属性

c - 使用 Write() 和 snprintf() 以原子方式写入文件

file - 如何在 julia 中编辑一行文件?

haskell - 如何在 Haskell 中组合句柄?

blackberry - 黑莓编程自动旋转

java - BlackBerry 6.0 中的 LocationProvider 超时?

blackberry - 具有不同图像和可点击事件的父级和子级节点 - Treeview Blackberry

黑莓在删除应用程序时删除 SD 卡文件

java - 将链接列表保存到文件中的单独行中