XmlUtils { public static Document toXmlDoc(String xml) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); DtdEntityResolver dtdEntityResolver = new DtdEntityResolver(); builder.setEntityResolver(dtdEntityResolver); InputStream is = FileUtils.toInputStream(xml); Document doc = builder.parse(is); if (dtdEntityResolver.dtdPresent) { return toXmlDoc(toString(doc, false)); } else { return doc; } } catch (Exception e) { throw new RuntimeException(e); } } private  XmlUtils(); static String toString(Node node); static String toString(Node node, boolean pretty); static void trimWhiteSpace(Node node); static Document toXmlDoc(String xml); static NodeList getNodeListByPath(Node node, String path); static String stripNameSpacePrefixes(String path); static Node getNodeByPath(Node node, String path, boolean create); static Node createNodeByPath(Document doc, String path); static String getTextValueByPath(Node node, String path); static void setByPath(Node doc, String path, String value); static void removeByPath(Document doc, String path); static void setByPath(Document doc, String path, Node in); static DocumentContext toJsonDoc(Node node); static int getChildElementCount(Node node); static Object toObject(Node node); static Object toObject(Node node, boolean removeNamespace); static Document fromMap(Map<String, Object> map); static Document fromObject(String name, Object o); static List<Element> fromObject(Document doc, String name, Object o); static Document newDocument(); static void addAttributes(Element element, Map<String, Object> map); static Element createElement(Node node, String name, String value, Map<String, Object> attributes); static Document toNewDocument(Node in); static Document toXmlDoc(Object o); static String toXml(Object o);  }
@Test public void testParsing() { String xml = "<foo></foo>"; Document doc = XmlUtils.toXmlDoc(xml); String rootName = doc.getDocumentElement().getNodeName(); assertEquals("foo", rootName); }