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.

3
  • And after you changed it - did you save it back to file ? Commented Jul 22, 2014 at 5:10
  • You are chaining its in-memory value, you will need to write the value out to the file to achieve what you want. Commented Jul 22, 2014 at 5:11
  • Yes i have saved the XML file again after writing but then too the value of tag is not changed Commented Jul 22, 2014 at 5:12

2 Answers 2

0

Try this..

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

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

Please check this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.