从String解析xml

时间:2010-07-07 16:41:58

标签: java xml blackberry

我正在解析数据来自String的xml。我正在使用以下代码进行解析: -

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
    .newInstance();
    DocumentBuilder docBuilder = docBuilderFactory
    .newDocumentBuilder();
    docBuilder.isValidating();
    ByteArrayInputStream ba= new ByteArrayInputStream(connect.content.getBytes("UTF-8"));
    doc = docBuilder.parse(ba);
    doc.getDocumentElement().normalize();
    NodeList locationStatus = doc.getElementsByTagName("street");
    for (int i = 0; i < locationStatus.getLength(); i++) {
        Node locationValue = locationStatus.item(i).getChildNodes().item(0);
        _node.addElement(locationValue.getNodeValue());
    }      
    String [] nodeString = new String[_node.size()];
    _node.copyInto(nodeString);
    add(new LabelField(nodeString.length+""));

但是这段代码给出了错误,我不知道错误在哪里。

请帮帮我

提前致谢

2 个答案:

答案 0 :(得分:0)

使用XStream或JAXB

答案 1 :(得分:0)

你尝试过这种方式吗?

SAXParserImpl saxparser = new SAXParserImpl();
ResponseHandler handler = new ResponseHandler();
ByteArrayInputStream stream = new ByteArrayInputStream(xmlresp.getBytes());

public class ResponseHandler extends DefaultHandler
{
  public void startElement(String uri, String localName, 
                           String qName,Attributes attributes) {}
public void characters(char[] ch, int start, int length)
        {          
            tempVal = new String(ch, start, length);
        }
public void endElement(String uri, String localName, 
                       String qName) throws SAXException    {}
}