Java Code Examples for org.w3c.dom.Document#renameNode()
The following examples show how to use
org.w3c.dom.Document#renameNode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RenameOperationServiceImpl.java From cs-actions with Apache License 2.0 | 6 votes |
/** * Renames the tag or attribute of an XML (provided as String or file) at a given XPath. * * @param inputs inputs * @return a String representation of the modified XML * @throws Exception in case something goes wrong */ @Override public String execute(EditXmlInputs inputs) throws Exception { Document doc = XmlUtils.createDocument(inputs.getXml(), inputs.getFilePath(), inputs.getParsingFeatures()); NodeList nodeList = XmlUtils.readNode(doc, inputs.getXpath1(), XmlUtils.getNamespaceContext(inputs.getXml(), inputs.getFilePath())); Node node; for (int i = 0; i < nodeList.getLength(); i++) { node = nodeList.item(i); if (Constants.Inputs.TYPE_ELEM.equals(inputs.getType())) { doc.renameNode(node, node.getNamespaceURI(), inputs.getValue()); } else if (Constants.Inputs.TYPE_ATTR.equals(inputs.getType())) { String attributeValue = ((Element) node).getAttribute(inputs.getName()); if ((attributeValue != null) && (!StringUtils.isEmpty(attributeValue))) { ((Element) node).removeAttribute(inputs.getName()); ((Element) node).setAttribute(inputs.getValue(), attributeValue); } } } return DocumentUtils.documentToString(doc); }
Example 2
Source File: XMLHierarchy.java From android-uiautomator-server with MIT License | 5 votes |
private static void visitNode(Node node, HashMap<String, Integer> instances) { Document doc = node.getOwnerDocument(); NamedNodeMap attributes = node.getAttributes(); String androidClass; try { androidClass = attributes.getNamedItem("class").getNodeValue(); } catch (Exception e) { return; } androidClass = cleanTagName(androidClass); if (!instances.containsKey(androidClass)) { instances.put(androidClass, 0); } Integer instance = instances.get(androidClass); Node attrNode = doc.createAttribute("instance"); attrNode.setNodeValue(instance.toString()); attributes.setNamedItem(attrNode); doc.renameNode(node, node.getNamespaceURI(), androidClass); instances.put(androidClass, instance + 1); }
Example 3
Source File: XmlUtils.java From OpenEstate-IO with Apache License 2.0 | 5 votes |
/** * Replace the namespace of a {@link Node} and its children. * * @param doc the document to update * @param node the node to update * @param newNamespaceURI the new namespace URI */ public static void replaceNamespace(Document doc, Node node, String newNamespaceURI) { if (node instanceof Attr) { doc.renameNode(node, newNamespaceURI, node.getLocalName()); } else if (node instanceof Element) { doc.renameNode(node, newNamespaceURI, node.getLocalName()); NodeList children = node.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { XmlUtils.replaceNamespace(doc, children.item(i), newNamespaceURI); } } }
Example 4
Source File: MaintainableXMLConversionServiceImpl.java From rice with Educational Community License v2.0 | 5 votes |
private void transformClassNode(Document document, Node node) throws ClassNotFoundException, XPathExpressionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException { String className = node.getNodeName(); if(this.classNameRuleMap.containsKey(className)) { String newClassName = this.classNameRuleMap.get(className); document.renameNode(node, null, newClassName); className = newClassName; } Class<?> dataObjectClass = Class.forName(className); if(classPropertyRuleMap.containsKey(className)) { transformNode(document, node, dataObjectClass, classPropertyRuleMap.get(className)); } transformNode(document, node, dataObjectClass, classPropertyRuleMap.get("*")); }
Example 5
Source File: MaintainableXMLConversionServiceImpl.java From rice with Educational Community License v2.0 | 5 votes |
private void transformClassNode(Document document, Node node) throws ClassNotFoundException, XPathExpressionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException { String className = node.getNodeName(); if(this.classNameRuleMap.containsKey(className)) { String newClassName = this.classNameRuleMap.get(className); document.renameNode(node, null, newClassName); className = newClassName; } Class<?> dataObjectClass = Class.forName(className); if(classPropertyRuleMap.containsKey(className)) { transformNode(document, node, dataObjectClass, classPropertyRuleMap.get(className)); } transformNode(document, node, dataObjectClass, classPropertyRuleMap.get("*")); }
Example 6
Source File: UserController.java From openjdk-jdk9 with GNU General Public License v2.0 | 4 votes |
/** * Checking conflicting namespaces and use renameNode and normalizeDocument. * @see <a href="content/accountInfo.xml">accountInfo.xml</a> * * @throws Exception If any errors occur. */ @Test public void testAddUser() throws Exception { String resultFile = USER_DIR + "accountRole.out"; String xmlFile = XML_DIR + "accountInfo.xml"; // Copy schema for outputfile Files.copy(Paths.get(XML_DIR, "accountInfo.xsd"), Paths.get(USER_DIR, "accountInfo.xsd"), StandardCopyOption.REPLACE_EXISTING); MyErrorHandler eh = new MyErrorHandler(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA_NS_URI); dbf.setNamespaceAware(true); dbf.setValidating(true); DocumentBuilder docBuilder = dbf.newDocumentBuilder(); docBuilder.setErrorHandler(eh); Document document = docBuilder.parse(xmlFile); Element sell = (Element) document.getElementsByTagNameNS(PORTAL_ACCOUNT_NS, "Sell").item(0); Element role = (Element) sell.getParentNode(); Element buy = (Element) document.renameNode(sell, PORTAL_ACCOUNT_NS, "acc:Buy"); role.appendChild(buy); DOMImplementationLS impl = (DOMImplementationLS) DOMImplementationRegistry .newInstance().getDOMImplementation("LS"); LSSerializer writer = impl.createLSSerializer(); try(FileOutputStream output = new FileOutputStream(resultFile)) { MyDOMOutput mydomoutput = new MyDOMOutput(); mydomoutput.setByteStream(output); writer.write(document, mydomoutput); } docBuilder.parse(resultFile); assertFalse(eh.isAnyError()); }