DOMParserUtil { public static Node createNodeAtPosition(Node containerNode, String nodeToCreateName, String nodeContent, Integer position) { Node toReturn = containerNode.getOwnerDocument().createElement(nodeToCreateName); if (nodeContent != null) { toReturn.setTextContent(nodeContent); } if (containerNode.hasChildNodes() && position != null && position < containerNode.getChildNodes().getLength()) { Node positionNode = containerNode.getChildNodes().item(position); containerNode.insertBefore(toReturn, positionNode); } else { containerNode.appendChild(toReturn); } return toReturn; } 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 createNodeAtPosition() throws Exception { String newNodeName = "NEW_NODE_NAME_0"; String newNodeValue = "NEW_NODE_VALUE_="; Document document = DOMParserUtil.getDocument(XML); Map<Node, List<Node>> testNodesMap = DOMParserUtil.getChildrenNodesMap(document, MAIN_NODE, TEST_NODE); assertEquals(1, testNodesMap.size()); Node mainNode = testNodesMap.keySet().iterator().next(); Node retrieved = DOMParserUtil.createNodeAtPosition(mainNode, newNodeName, newNodeValue, null); assertNotNull(retrieved); assertEquals(newNodeName, retrieved.getNodeName()); assertEquals(newNodeValue, retrieved.getTextContent()); assertEquals(retrieved, mainNode.getChildNodes().item(mainNode.getChildNodes().getLength() - 1)); newNodeName = "NEW_NODE_NAME_1"; newNodeValue = "NEW_NODE_VALUE_1"; retrieved = DOMParserUtil.createNodeAtPosition(mainNode, newNodeName, newNodeValue, 0); assertNotNull(retrieved); assertEquals(newNodeName, retrieved.getNodeName()); assertEquals(newNodeValue, retrieved.getTextContent()); assertEquals(retrieved, mainNode.getChildNodes().item(0)); newNodeName = "NEW_NODE_NAME_2"; newNodeValue = "NEW_NODE_VALUE_2"; retrieved = DOMParserUtil.createNodeAtPosition(mainNode, newNodeName, newNodeValue, 2); assertNotNull(retrieved); assertEquals(newNodeName, retrieved.getNodeName()); assertEquals(newNodeValue, retrieved.getTextContent()); assertEquals(retrieved, mainNode.getChildNodes().item(2)); }