@Test public void testAddNodeRecursively() throws Exception { InputStream newXml; int numNodes; newXml = getSampleXml(true); numNodes = classUnderTest.addNodeRecursively(newXml, XML_SIGNATURE_XPATH, false); assertEquals(3, numNodes, 0.0); assertEquals(3, classUnderTest.getNodes(XML_SIGNATURE_XPATH).getLength(), 0.0); newXml = getSampleXml(true); numNodes = classUnderTest.addNodeRecursively(newXml, XML_SIGNATURE_XPATH, false); assertEquals(3, classUnderTest.getNodes(XML_SIGNATURE_XPATH).getLength(), 0.0); assertEquals(0, numNodes, 0.0); newXml = getSampleXml(true); numNodes = classUnderTest.addNodeRecursively(newXml, XML_SIGNATURE_XPATH, true); assertEquals(6, classUnderTest.getNodes(XML_SIGNATURE_XPATH).getLength(), 0.0); assertEquals(3, numNodes, 0.0); }
public int addNodeRecursively(InputStream newXml, String xpath, boolean addDuplicates) throws XmlException { Log.debug("addNodeRecursively(..., {})", xpath); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); try { DocumentBuilder builder = dbf.newDocumentBuilder(); Document newXmlDoc = builder.parse(newXml); NodeList newNodes = getNodes(newXmlDoc, xpath); if (newNodes == null || newNodes.getLength() == 0) { Log.warn("No nodes found with XPath: " + xpath); return 0; } NodeList existingNodes = getNodes(doc, xpath); Node docElement = doc.getDocumentElement(); int count = 0; for (int k = 0; k < newNodes.getLength(); k++) { Node n = newNodes.item(k); if (addDuplicates || !contains(existingNodes, n)) { Node firstDocImportedNode = doc.importNode(n, true); docElement.appendChild(firstDocImportedNode); ++count; } } return count; } catch (Exception e) { Log.warn("addNodeRecursively", e); throw new XmlException(e); } }
Xml { public int addNodeRecursively(InputStream newXml, String xpath, boolean addDuplicates) throws XmlException { Log.debug("addNodeRecursively(..., {})", xpath); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); try { DocumentBuilder builder = dbf.newDocumentBuilder(); Document newXmlDoc = builder.parse(newXml); NodeList newNodes = getNodes(newXmlDoc, xpath); if (newNodes == null || newNodes.getLength() == 0) { Log.warn("No nodes found with XPath: " + xpath); return 0; } NodeList existingNodes = getNodes(doc, xpath); Node docElement = doc.getDocumentElement(); int count = 0; for (int k = 0; k < newNodes.getLength(); k++) { Node n = newNodes.item(k); if (addDuplicates || !contains(existingNodes, n)) { Node firstDocImportedNode = doc.importNode(n, true); docElement.appendChild(firstDocImportedNode); ++count; } } return count; } catch (Exception e) { Log.warn("addNodeRecursively", e); throw new XmlException(e); } } }
Xml { public int addNodeRecursively(InputStream newXml, String xpath, boolean addDuplicates) throws XmlException { Log.debug("addNodeRecursively(..., {})", xpath); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); try { DocumentBuilder builder = dbf.newDocumentBuilder(); Document newXmlDoc = builder.parse(newXml); NodeList newNodes = getNodes(newXmlDoc, xpath); if (newNodes == null || newNodes.getLength() == 0) { Log.warn("No nodes found with XPath: " + xpath); return 0; } NodeList existingNodes = getNodes(doc, xpath); Node docElement = doc.getDocumentElement(); int count = 0; for (int k = 0; k < newNodes.getLength(); k++) { Node n = newNodes.item(k); if (addDuplicates || !contains(existingNodes, n)) { Node firstDocImportedNode = doc.importNode(n, true); docElement.appendChild(firstDocImportedNode); ++count; } } return count; } catch (Exception e) { Log.warn("addNodeRecursively", e); throw new XmlException(e); } } Xml(String source);  Xml(InputStream source);  Xml(Document doc); }
Xml { public int addNodeRecursively(InputStream newXml, String xpath, boolean addDuplicates) throws XmlException { Log.debug("addNodeRecursively(..., {})", xpath); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); try { DocumentBuilder builder = dbf.newDocumentBuilder(); Document newXmlDoc = builder.parse(newXml); NodeList newNodes = getNodes(newXmlDoc, xpath); if (newNodes == null || newNodes.getLength() == 0) { Log.warn("No nodes found with XPath: " + xpath); return 0; } NodeList existingNodes = getNodes(doc, xpath); Node docElement = doc.getDocumentElement(); int count = 0; for (int k = 0; k < newNodes.getLength(); k++) { Node n = newNodes.item(k); if (addDuplicates || !contains(existingNodes, n)) { Node firstDocImportedNode = doc.importNode(n, true); docElement.appendChild(firstDocImportedNode); ++count; } } return count; } catch (Exception e) { Log.warn("addNodeRecursively", e); throw new XmlException(e); } } Xml(String source);  Xml(InputStream source);  Xml(Document doc); String getValue(String xpath); NodeList getNodes(String xpath); Node addNode(Node parent, String newNode); void removeNodes(String xpath); @Override String toString(); String toString(String xpath); String toPrettyString(String xpath); static Node addNodeIfNotPresent(Node parent, String newNode); static NodeList getNodes(Node node, String xpath); static Node getFirstNode(Node node, String xpath); String getAttributeValue(String xpathToNode, String attribName); static String getAttribute(Node node, String name); int addNodeRecursively(InputStream newXml, String xpath, boolean addDuplicates); static boolean contains(NodeList list, Node node); Document getDocument(); void toOutputStream(OutputStream os); }
Xml { public int addNodeRecursively(InputStream newXml, String xpath, boolean addDuplicates) throws XmlException { Log.debug("addNodeRecursively(..., {})", xpath); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); try { DocumentBuilder builder = dbf.newDocumentBuilder(); Document newXmlDoc = builder.parse(newXml); NodeList newNodes = getNodes(newXmlDoc, xpath); if (newNodes == null || newNodes.getLength() == 0) { Log.warn("No nodes found with XPath: " + xpath); return 0; } NodeList existingNodes = getNodes(doc, xpath); Node docElement = doc.getDocumentElement(); int count = 0; for (int k = 0; k < newNodes.getLength(); k++) { Node n = newNodes.item(k); if (addDuplicates || !contains(existingNodes, n)) { Node firstDocImportedNode = doc.importNode(n, true); docElement.appendChild(firstDocImportedNode); ++count; } } return count; } catch (Exception e) { Log.warn("addNodeRecursively", e); throw new XmlException(e); } } Xml(String source);  Xml(InputStream source);  Xml(Document doc); String getValue(String xpath); NodeList getNodes(String xpath); Node addNode(Node parent, String newNode); void removeNodes(String xpath); @Override String toString(); String toString(String xpath); String toPrettyString(String xpath); static Node addNodeIfNotPresent(Node parent, String newNode); static NodeList getNodes(Node node, String xpath); static Node getFirstNode(Node node, String xpath); String getAttributeValue(String xpathToNode, String attribName); static String getAttribute(Node node, String name); int addNodeRecursively(InputStream newXml, String xpath, boolean addDuplicates); static boolean contains(NodeList list, Node node); Document getDocument(); void toOutputStream(OutputStream os);  }