Here is my XML:
<Connections>
<Connection>
<userName>infadf</userName>
<password>tcslkvcvo@123</password>
<schemaName>dbo</schemaName>
<status>OLD</status>
<portNum>600687</portNum>
<aliasName>first</aliasName>
</Connection>
</Connections>
I want to change value of tag "status" to "NEW".
Below is my java code:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = null;
try
{
dBuilder = dbFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(new File(path).exists())
{
Document doc = null;
try {
doc = dBuilder.parse(path);
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("Connection");
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node nNode = nList.item(temp);
Element eElement = (Element) nNode;
if (nNode.getNodeType() == Node.ELEMENT_NODE)
{
}
if(eElement.getElementsByTagName("status").item(0).getTextContent().equalsIgnoreCase("OLD"))
{
eElement.getElementsByTagName("status").item(0).setTextContent("NEW");
}
}
}
The value of status tag is not getting changed in XML. Please help.