I want to appened an attribute to an existing xml using stax parse. Please suggest.
Below is the snippnet code which I need to appened.
<un:UtranCell id="RNC17-1-1">
It as to look like as follow after appending
<un:UtranCell id="RNC17-1-1" modifier="delete">
Below is snippnet code i tried. but i am failied to add the attribute
try {
File fXmlFile = new File("/home/xgeoraj/bcgImportFiles/imports/UtranCell.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("un:UtranCell");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("UtranCell id is: " + eElement.getAttribute("id"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Below is the XML file in which i need to add it.
<?xml version="1.0" encoding="UTF-8"?>
<bulkCmConfigDataFile xmlns:un="utranNrm.xsd" xmlns:xn="genericNrm.xsd" xmlns:gn="geranNrm.xsd" xmlns="configData.xsd" xmlns:es="EricssonSpecificAttributes.14.02.xsd">
<fileHeader fileFormatVersion="32.615 V4.5" vendorName="Ericsson"/>
<configData dnPrefix="Undefined">
<xn:SubNetwork id="ONRM_ROOT_MO_R">
<xn:SubNetwork id="RNC17">
<xn:MeContext id="RNC17">
<xn:ManagedElement id="1">
<un:RncFunction id="1">
<un:UtranCell id="RNC17-1-1" modifier="delete">
Adding an attribute to the element you have retrieved which is what I think you want to do is done by setting an element. taking out the if-statement.
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("UtranCell id is: " + eElement.getAttribute("id"));
eElement.setAttribute("modifier", "delete"); // <-- This is the added line
}
For a reference see the javadoc for Element .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.