@Test public void testXPathExistsWithNamespaces() throws Exception { String xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<soap:Envelope xmlns:soap='http: + " <soap:Header>" + " <ResponseHeader xmlns='https: + " <requestId>123456</requestId>" + " <serviceName>BatchJobService</serviceName>" + " <methodName>mutate</methodName>" + " <operations>1</operations>" + " <responseTime>247</responseTime>" + " </ResponseHeader>" + " </soap:Header>" + "</soap:Envelope>"; DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); Document document = documentBuilderFactory .newDocumentBuilder() .parse(Streams.wrapString(xml, StandardCharsets.UTF_8)); Node node = nodeExtractor.extractNode( document, Lists.newArrayList("Envelope", "Header", "ResponseHeader", "requestId")); assertNotNull(node); assertEquals( "Given an xpath in the XML, should return its node", "123456", node.getFirstChild().getNodeValue()); }
public Node extractNode(@Nullable Node parentNode, Iterable<String> xPath) { Preconditions.checkNotNull(xPath, "Null xpath list"); Node node = parentNode; boolean wasMatch = false; Iterator<String> xPathElementsIter = xPath.iterator(); while (xPathElementsIter.hasNext() && node != null) { wasMatch = false; String localName = xPathElementsIter.next(); NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); String childName = childNode.getLocalName(); if (childName == null || childName.contains(":")) { childName = childNode.getNodeName(); } if (localName.equals(childName)) { node = childNode; wasMatch = true; break; } } } return wasMatch ? node : null; }
NodeExtractor { public Node extractNode(@Nullable Node parentNode, Iterable<String> xPath) { Preconditions.checkNotNull(xPath, "Null xpath list"); Node node = parentNode; boolean wasMatch = false; Iterator<String> xPathElementsIter = xPath.iterator(); while (xPathElementsIter.hasNext() && node != null) { wasMatch = false; String localName = xPathElementsIter.next(); NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); String childName = childNode.getLocalName(); if (childName == null || childName.contains(":")) { childName = childNode.getNodeName(); } if (localName.equals(childName)) { node = childNode; wasMatch = true; break; } } } return wasMatch ? node : null; } }
NodeExtractor { public Node extractNode(@Nullable Node parentNode, Iterable<String> xPath) { Preconditions.checkNotNull(xPath, "Null xpath list"); Node node = parentNode; boolean wasMatch = false; Iterator<String> xPathElementsIter = xPath.iterator(); while (xPathElementsIter.hasNext() && node != null) { wasMatch = false; String localName = xPathElementsIter.next(); NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); String childName = childNode.getLocalName(); if (childName == null || childName.contains(":")) { childName = childNode.getNodeName(); } if (localName.equals(childName)) { node = childNode; wasMatch = true; break; } } } return wasMatch ? node : null; }  }
NodeExtractor { public Node extractNode(@Nullable Node parentNode, Iterable<String> xPath) { Preconditions.checkNotNull(xPath, "Null xpath list"); Node node = parentNode; boolean wasMatch = false; Iterator<String> xPathElementsIter = xPath.iterator(); while (xPathElementsIter.hasNext() && node != null) { wasMatch = false; String localName = xPathElementsIter.next(); NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); String childName = childNode.getLocalName(); if (childName == null || childName.contains(":")) { childName = childNode.getNodeName(); } if (localName.equals(childName)) { node = childNode; wasMatch = true; break; } } } return wasMatch ? node : null; }  String extractNodeValue(@Nullable Node parentNode, Iterable<String> xPath); Node extractNode(@Nullable Node parentNode, Iterable<String> xPath); }
NodeExtractor { public Node extractNode(@Nullable Node parentNode, Iterable<String> xPath) { Preconditions.checkNotNull(xPath, "Null xpath list"); Node node = parentNode; boolean wasMatch = false; Iterator<String> xPathElementsIter = xPath.iterator(); while (xPathElementsIter.hasNext() && node != null) { wasMatch = false; String localName = xPathElementsIter.next(); NodeList childNodes = node.getChildNodes(); for (int i = 0; i < childNodes.getLength(); i++) { Node childNode = childNodes.item(i); String childName = childNode.getLocalName(); if (childName == null || childName.contains(":")) { childName = childNode.getNodeName(); } if (localName.equals(childName)) { node = childNode; wasMatch = true; break; } } } return wasMatch ? node : null; }  String extractNodeValue(@Nullable Node parentNode, Iterable<String> xPath); Node extractNode(@Nullable Node parentNode, Iterable<String> xPath);  }