XmlUtil { public static Document createDOMDocument(String xml) throws ProblemException { if (Checker.isBlank(xml)) return null; try { InputSource is = new InputSource(new StringReader(xml)); return getDocBuilder().parse(is); } catch (IOException | SAXException e) { logger.error(e.toString(), e); throw new ProblemException(e, XmlProblem.PARSING, e.toString()); } }  static Document createDom(); static Document createDOMDocument(String xml); static String prettyFormatXml(String xml, String encoding); static String domToString(Node node, String encoding, boolean pretty); static Node findNode(Node document, String xPathExpression); static Element getFirstElement(Node node); static boolean containsElements(NodeList nodeList); static XMLGregorianCalendar newXMLGregorianCalendar(Date date); static T unmarshal(Class<T> targetClass, String xml); static String marshal(T object); static Source getSource(String xml);  }
@Test public void testCreateDOMDocument() throws Exception { final String xml = "<root><elem attr=\"value\">content</elem></root>"; Document doc = XmlUtil.createDOMDocument(xml); Node rootElement = doc.getFirstChild(); assertEquals("root", rootElement.getNodeName()); Node elem = rootElement.getFirstChild(); assertEquals("elem", elem.getNodeName()); assertEquals(1, elem.getAttributes().getLength()); assertEquals("value", (elem.getAttributes().getNamedItem("attr")).getNodeValue()); assertEquals("content", elem.getTextContent()); Document emptyDoc = XmlUtil.createDOMDocument(""); assertNull(emptyDoc); }