XMLUtils { public static Node findNodeByName(Document doc, String pathExpression) { final StringTokenizer tok = new StringTokenizer(pathExpression, "."); final int numToks = tok.countTokens(); NodeList elements; if (numToks == 1) { elements = doc.getElementsByTagNameNS("*", pathExpression); return elements.item(0); } String element = pathExpression.substring(pathExpression.lastIndexOf('.')+1); elements = doc.getElementsByTagNameNS("*", element); String attributeName = null; if (elements.getLength() == 0) { attributeName = element; Node found = findNodeByName(doc, pathExpression.substring(0, pathExpression.length() - attributeName.length() - 1)); if (found != null) { return found.getAttributes().getNamedItem(attributeName); } else { return null; } } StringBuffer pathName; Node parent; for (int j=0; j<elements.getLength(); j++) { int cnt = numToks-1; pathName = new StringBuffer(element); parent = elements.item(j).getParentNode(); do { if (parent != null) { pathName.insert(0, '.'); pathName.insert(0, parent.getLocalName()); parent = parent.getParentNode(); } } while (parent != null && --cnt > 0); if (pathName.toString().equals(pathExpression)) {return elements.item(j);} } return null; } private  XMLUtils(); static void initialize(XmlConfigurer xmlConfigurer); static LSParser createLSParser(); static LSSerializer createLSSerializer(); static LSInput createLSInput(); static LSOutput createLSOutput(); static Node findNodeByName(Document doc, String pathExpression); static void stripWhitespaceNodes(Node element); static String getNodesPathName(Node node); static String serialize(Document doc); static String prettyPrint(String xml); static Map<String, String> lookupNamespaces(Node referenceNode); static Document parseMessagePayload(String messagePayload); static Charset getTargetCharset(Document doc); static String omitXmlDeclaration(String xml); static boolean hasXmlPayload(Message message);  }
@Test public void testFindNodeByName() { Document doc = XMLUtils.parseMessagePayload( "<testRequest><message id=\"1\">Hello</message></testRequest>"); Node result; result = XMLUtils.findNodeByName(doc, "testRequest"); Assert.assertNotNull(result); Assert.assertEquals(result.getLocalName(), "testRequest"); Assert.assertEquals(result.getNodeType(), Document.ELEMENT_NODE); result = XMLUtils.findNodeByName(doc, "testRequest.message"); Assert.assertNotNull(result); Assert.assertEquals(result.getLocalName(), "message"); Assert.assertEquals(result.getNodeType(), Document.ELEMENT_NODE); result = XMLUtils.findNodeByName(doc, "testRequest.message.id"); Assert.assertNotNull(result); Assert.assertEquals(result.getLocalName(), "id"); Assert.assertEquals(result.getNodeType(), Document.ATTRIBUTE_NODE); result = XMLUtils.findNodeByName(doc, "testRequest.wrongElement"); Assert.assertNull(result); }