Friday, October 24, 2008

Parse a string of XML into a DOM Document object

Java code:

String xmlStr = "something ";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();

//This handles string containg special characters (UTF8):
Document doc = builder.parse(new InputSource(new StringReader(xmlStr)));

//This cannot handle UTF8
InputStream is = new ByteArrayInputStream(content.getBytes());
Document doc = builder.parse(is);

0 Comments:

Post a Comment

<< Home