@Test public void replaceNodeName() throws Exception { final String replacement = "replacement"; Document document = DOMParserUtil.getDocument(XML); DOMParserUtil.replaceNodeName(document, MAIN_NODE, TEST_NODE, replacement); final Map<Node, List<Node>> retrieved = DOMParserUtil.getChildrenNodesMap(document, MAIN_NODE, replacement); assertNotNull(retrieved); assertEquals(1, retrieved.size()); List<Node> testNodes = retrieved.values().iterator().next(); assertNotNull(testNodes); assertEquals(1, testNodes.size()); assertEquals("replacement", testNodes.get(0).getNodeName()); }
public static String replaceNodeName(Document document, String containerNodeName, String childNodeNameToReplace, String childNodeNameReplacement) throws Exception { final NodeList containerNodes = document.getElementsByTagName(containerNodeName); if (containerNodes != null) { for (int i = 0; i < containerNodes.getLength(); i++) { Node containerNode = containerNodes.item(i); final NodeList childNodes = containerNode.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); if (Objects.equals(childNode.getNodeName(), childNodeNameToReplace)) { document.renameNode(childNode, null, childNodeNameReplacement); } } } } return getString(document); }
DOMParserUtil { public static String replaceNodeName(Document document, String containerNodeName, String childNodeNameToReplace, String childNodeNameReplacement) throws Exception { final NodeList containerNodes = document.getElementsByTagName(containerNodeName); if (containerNodes != null) { for (int i = 0; i < containerNodes.getLength(); i++) { Node containerNode = containerNodes.item(i); final NodeList childNodes = containerNode.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); if (Objects.equals(childNode.getNodeName(), childNodeNameToReplace)) { document.renameNode(childNode, null, childNodeNameReplacement); } } } } return getString(document); } }
DOMParserUtil { public static String replaceNodeName(Document document, String containerNodeName, String childNodeNameToReplace, String childNodeNameReplacement) throws Exception { final NodeList containerNodes = document.getElementsByTagName(containerNodeName); if (containerNodes != null) { for (int i = 0; i < containerNodes.getLength(); i++) { Node containerNode = containerNodes.item(i); final NodeList childNodes = containerNode.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); if (Objects.equals(childNode.getNodeName(), childNodeNameToReplace)) { document.renameNode(childNode, null, childNodeNameReplacement); } } } } return getString(document); } private  DOMParserUtil(); }
DOMParserUtil { public static String replaceNodeName(Document document, String containerNodeName, String childNodeNameToReplace, String childNodeNameReplacement) throws Exception { final NodeList containerNodes = document.getElementsByTagName(containerNodeName); if (containerNodes != null) { for (int i = 0; i < containerNodes.getLength(); i++) { Node containerNode = containerNodes.item(i); final NodeList childNodes = containerNode.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); if (Objects.equals(childNode.getNodeName(), childNodeNameToReplace)) { document.renameNode(childNode, null, childNodeNameReplacement); } } } } return getString(document); } 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); }
DOMParserUtil { public static String replaceNodeName(Document document, String containerNodeName, String childNodeNameToReplace, String childNodeNameReplacement) throws Exception { final NodeList containerNodes = document.getElementsByTagName(containerNodeName); if (containerNodes != null) { for (int i = 0; i < containerNodes.getLength(); i++) { Node containerNode = containerNodes.item(i); final NodeList childNodes = containerNode.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node childNode = childNodes.item(j); if (Objects.equals(childNode.getNodeName(), childNodeNameToReplace)) { document.renameNode(childNode, null, childNodeNameReplacement); } } } } return getString(document); } 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);  }