This helper function will read a xml string (or any InputStream) and create a Document from this.
public static Document loadXMLAsDom(String xml){ return loadXMLAsDom(new ByteArrayInputStream(xml.getBytes())); } public static Document loadXMLAsDom(InputStream inputStream) { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); DocumentBuilder documentBuilder = null; Document document = null; try{ documentBuilder = documentBuilderFactory.newDocumentBuilder(); document = documentBuilder.parse(new InputSource(inputStream)); //use InputSource here to get better support for encodings inputStream.close(); } catch (ParserConfigurationException e){ System.out.println("loadXMLAsDom got a ParserConfigurationException! "); e.printStackTrace; } catch (IOException e){ System.out.println("loadXMLAsDom got a IOException! "); e.printStackTrace; } catch (SAXException e){ System.out.println("loadXMLAsDom got a SAXException! "); e.printStackTrace; } return document; } |