2010-10-25 41 views
2

我想用以下代碼解析XML,但StringReader在BlackBerry JDE中不可用。什麼是正確的方法來做到這一點?在BlackBerry上解析XML字符串

  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder db = dbf.newDocumentBuilder(); 
      InputSource is = new InputSource(); 
      is.setCharacterStream(new StringReader(xmlRecords)); 
      Document doc = db.parse(is); 

回答

2
String xmlString = "<xml> </xml>" // your xml string  

ByteArrayInputStream bis = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); 

Document doc = builder.parse(bis); 

如果你想建立從數據來自服務器一個DOM嘗試了這一點

2

,你好得多直接用的DocumentBuilder解析InputStream的,而不是將數據讀入一個字符串,並試圖與此合作。一種方法是:

Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);