/** * Convenience method for getting the value of a node, handling the case where the node does not * exist. * * @return the value of the node at {@code xPath} if it exists, else {@code null}. */ public String extractNodeValue(@Nullable Node parentNode, Iterable<String> xPath) { Node node = extractNode(parentNode, xPath); if (node == null || node.getFirstChild() == null) { return null; } return node.getFirstChild().getNodeValue(); }
/** * Convenience method for getting the value of a node, handling the case where the node does not * exist. * * @return the value of the node at {@code xPath} if it exists, else {@code null}. */ public String extractNodeValue(@Nullable Node parentNode, Iterable<String> xPath) { Node node = extractNode(parentNode, xPath); if (node == null || node.getFirstChild() == null) { return null; } return node.getFirstChild().getNodeValue(); }
@Test public void testNullParentNode() { Node node = nodeExtractor.extractNode(null, Lists.newArrayList("Foo")); assertNull("Given a null parent node, should return null", node); }
@Test public void testXPathExistsMultipleTimesWithoutNamespaces() throws Exception { String xml = "<Header><requestId>123456</requestId><requestId>78910</requestId></Header>"; Document document = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(Streams.wrapString(xml, StandardCharsets.UTF_8)); Node node = nodeExtractor.extractNode(document, Lists.newArrayList("Header", "requestId")); assertEquals( "Given an xpath in the XML, should return its node", "123456", node.getFirstChild().getNodeValue()); }
@Test public void testXPathExistsWithoutNamespaces() throws Exception { String xml = "<Header><requestId>123456</requestId></Header>"; Document document = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(Streams.wrapString(xml, StandardCharsets.UTF_8)); Node node = nodeExtractor.extractNode(document, Lists.newArrayList("Header", "requestId")); assertEquals( "Given an xpath in the XML, should return its node", "123456", node.getFirstChild().getNodeValue()); }
@Test public void testXPathDoesNotExist() throws Exception { String xml = "<Header><requestId>123456</requestId></Header>"; Document document = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(Streams.wrapString(xml, StandardCharsets.UTF_8)); Node node = nodeExtractor.extractNode(document, Lists.newArrayList("Foo")); assertNull("Given an xpath not in the XML, should return null", node); }
@Test public void testXPathEmpty() throws Exception { String xml = "<Header><requestId>123456</requestId></Header>"; Document document = DocumentBuilderFactory.newInstance() .newDocumentBuilder() .parse(Streams.wrapString(xml, StandardCharsets.UTF_8)); Node node = nodeExtractor.extractNode(document, Collections.<String>emptyList()); assertNull("Given an empty xpath elements list, should return null", node); } }
@Test public void testNullXPaths_fails() { thrown.expect(NullPointerException.class); thrown.expectMessage("xpath"); nodeExtractor.extractNode(null, null); }
@Test public void testXPathExistsWithNamespaces() throws Exception { String xml = "<?xml version='1.0' encoding='UTF-8'?>" + "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" + " <soap:Header>" + " <ResponseHeader xmlns='https://adwords.google.com/api/adwords/cm/v209901'>" + " <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()); }