2010-07-07 196 views
-1

我解析一个xml数据来自一个字符串。我使用下面的代码解析: -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

什么是错误的细节?你有没有堆栈跟踪? – 2010-07-07 16:46:16

+1

你能发布错误消息和堆栈跟踪吗? – 2010-07-07 16:47:28

+1

它也将有助于查看您试图解析的XML – 2010-07-07 16:51:16

回答

0

使用XStream的或JAXB

+0

这甚至可以在BlackBerry上使用吗? – 2010-07-07 17:08:52

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 {} 
}