DOMParserUtil { public static Map<Node, Node> createNodes(Document document, String containerNodeName, String childNodeName, String nodeContent) { return asStream(document.getElementsByTagName(containerNodeName)) .collect(Collectors.toMap( containerNode -> containerNode, containerNode -> { Node childNode = document.createElement(childNodeName); containerNode.appendChild(childNode); if (nodeContent != null) { childNode.setTextContent(nodeContent); } return childNode; } )); } private  DOMParserUtil(); static String cleanupNodes(String fullXml, String containerNodeName, String nodeToRemoveName); static void cleanupNodes(Document document, String containerNodeName, String nodeToRemoveName); static void replaceNodeText(Document document, String containerNodeName, String nodeName, String toReplace, String replacement); static String replaceNodeName(Document document, String containerNodeName, String childNodeNameToReplace, String childNodeNameReplacement); static String getAttributeValue(Node containerNode, String attributeName); static Map<Node, String> getAttributeValues(Document document, String containerNodeName, String attributeName); static Map<Node, String> getAttributeValues(Document document, String attributeName); static void setAttributeValue(Document document, String containerNodeName, String attributeName, String attributeValue); static Map<Node, Node> createNodes(Document document, String containerNodeName, String childNodeName, String nodeContent); static Map<Node, Node> createNestedNodes(Document document, String mainContainerNodeName, String containerNodeName, String childNodeName, String nodeContent); static Node createNodeAtPosition(Node containerNode, String nodeToCreateName, String nodeContent, Integer position); static Map<Node, List<Node>> getChildrenNodesMap(String fullXml, String containerNodeName, String childNodeName); static Map<Node, List<Node>> getChildrenNodesMap(Document document, String containerNodeName, String childNodeName); static Map<Node, List<Node>> getChildrenNodesMap(Node node, String containerNodeName, String childNodeName); static List<Node> getChildrenNodesList(Node node, String childNodeName); static Map<Node, List<Node>> getNestedChildrenNodesMap(Document document, String mainContainerNodeName, String containerNodeName, String childNodeName); static List<Node> getNestedChildrenNodesList(Document document, String grandParentNodeName, String parentNodeName, String childNodeName); static List<Node> getNestedChildrenNodesList(Node node, String containerName, String childNodeName); static Document getDocument(String xml); static String getString(Document toRead);  }
@Test public void createNodes() throws Exception { final String newNodeName = "NEW_NODE_NAME"; final String newNodeValue = "NEW_NODE_VALUE"; Document document = DOMParserUtil.getDocument(XML); Map<Node, Node> retrieved = DOMParserUtil.createNodes(document, MAIN_NODE, newNodeName, newNodeValue); assertEquals(1, retrieved.size()); Node created = (Node) retrieved.values().toArray()[0]; assertNotNull(created); assertEquals(newNodeName, created.getNodeName()); assertEquals(newNodeValue, created.getTextContent()); retrieved = DOMParserUtil.createNodes(document, MAIN_NODE, newNodeName, null); assertEquals(1, retrieved.size()); created = (Node) retrieved.values().toArray()[0]; assertNotNull(created); assertEquals(newNodeName, created.getNodeName()); assertTrue(created.getTextContent().isEmpty()); retrieved = DOMParserUtil.createNodes(document, CHILD_NODE, newNodeName, newNodeValue); assertEquals(2, retrieved.size()); retrieved.forEach((key, createdNode) -> { assertNotNull(createdNode); assertEquals(newNodeName, createdNode.getNodeName()); assertEquals(newNodeValue, createdNode.getTextContent()); }); retrieved = DOMParserUtil.createNodes(document, CHILD_NODE, newNodeName, null); assertEquals(2, retrieved.size()); retrieved.forEach((key, createdNode) -> { assertNotNull(createdNode); assertEquals(newNodeName, createdNode.getNodeName()); assertTrue(createdNode.getTextContent().isEmpty()); }); }