Link to home
Start Free TrialLog in
Avatar of manish_regmi
manish_regmi

asked on

DOM java question

Hi all,

      I am saving my configuration file in XML. I retrieve and update the config file using DOM. I am able to retrieve the data but when i update the valuse changes in memory but not in file.

/* xml file */
<Configuration>
  // Some other stuffs here //
      <Config>
            <ip>192.168.3.120</ip>
            <port>1521</port>
            <database>hitech</database>
      </Config>
</Configuration>

method to update value is like this
       
        static final String NODENAME = "Config";

      public void setConfiguration(String configName, String data)
      {
            NodeList listOfTag = doc.getElementsByTagName(NODENAME);
            int totalTag = listOfTag.getLength();
            for(int i=0;i<totalTag;i++)
            {
                  Node fieldNode = listOfTag.item(i);
                  if(fieldNode.getNodeType()==Node.ELEMENT_NODE)
                  {
                        Element fieldElement = (Element)fieldNode;
                        NodeList tagList = fieldElement.getElementsByTagName(configName);
                        Element tagElement = (Element)tagList.item(0);
                        NodeList textTList = tagElement.getChildNodes();
                        ((Node)textTList.item(0)).setNodeValue(data);
                        System.out.println("Written " + data + " reread " + ((Node)textTList.item(0)).getNodeValue().trim());
                        break;
                  }
            }
      }

The change is only seen in memory but not in file.

Thanks in advance
Manish Regmi
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of manish_regmi
manish_regmi

ASKER

Thanks it worked.

regards
Manish Regmi