2010-07-07 194 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 {} 
}