views:

32

answers:

2

I am trying to parse XML with the following code, but StringReader is not available in the BlackBerry JDE. What is the right way to do this?

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xmlRecords));
            Document doc = db.parse(is);
A: 
String xmlString = "<xml> </xml>" // your xml string    

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

Document doc = builder.parse(bis);

Try this out

rupesh
+2  A: 

If you want to build a DOM from data coming from a server, you're much better off parsing the InputStream directly with a DocumentBuilder rather than reading the data into a String and trying to work with that. One way is:

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