0

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.

jmj
  • 237,923
  • 42
  • 401
  • 438
user3824693
  • 49
  • 2
  • 8

2 Answers2

0

Try this..

if("status".equalsIgnoreCase((nNode.getNodeName()))) { nNode.setTextContent("2000000"); }

jned29
  • 477
  • 12
  • 50
0

The piece of code which you have written is working.

May be the way you are checking weather its working or not is wrong. I have gone through this for checking the update on the XML what you have done.

You still have any doubts on what you did? I cannot post images because of my reputation score otherwise I would have shown the output what I got from eclipse.

Community
  • 1
  • 1
HookUp
  • 393
  • 1
  • 6
  • 20
  • Please check [this](http://i1252.photobucket.com/albums/hh567/jkiran809/StackOverFlow_JavaXML_zpsa346a017.png) – HookUp Jul 22 '14 at 06:48