XmlUtil { public static Element getFirstElement(Node node) { NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); if (childNode instanceof Element) { return (Element) childNode; } } return null; }  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 testGetFirstElement() throws ProblemException { { final String xml = "<root><elem attr=\"value\">content</elem></root>"; Document dom = XmlUtil.createDOMDocument(xml); Element element = XmlUtil.getFirstElement(dom); assertNotNull(element); assertEquals("root", element.getNodeName()); } { final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><elem attr=\"value\">content</elem></root>"; Document dom = XmlUtil.createDOMDocument(xml); Element element = XmlUtil.getFirstElement(dom); assertNotNull(element); assertEquals("root", element.getNodeName()); } { final String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!--komentar--><root><elem attr=\"value\">content</elem></root>"; Document dom = XmlUtil.createDOMDocument(xml); Element element = XmlUtil.getFirstElement(dom); assertNotNull(element); assertEquals("root", element.getNodeName()); } }