2

您好我正在尝试 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());
}
4

2 回答 2

2
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;

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

我得到了所有元素的价值,用这个代码/ * /

于 2012-10-04T11:06:19.540 回答
0

使用 Sax 解析进行 XML 解析。请参阅此链接:http ://stackoverflow.com/questions/11901822/xml-parsing-using-sax-for-blackberry/11914662#11914662

于 2012-10-05T05:11:10.117 回答