12 Replies Latest reply: Apr 23, 2008 12:39 AM by 807591 RSS

    Updating an XML attribute values.

    807591
      Hi all,

      I have a XML file as follows.
      <?xml version="1.0"?>
      
           <data>
                <key>468</key>
                <name>Java</name>
                <option>12</option>
           </data>
      I want to update/change a value of <option> tag in different values. Say for 100. So try this.
                  DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                  DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                  
                  Document doc = docBuilder.parse (new File("records.xml"));
      
                  Node ss = doc.createElement("option");
                  ss.setTextContent("100");
                  doc.getFirstChild().appendChild(ss);
      But this not update my xml file. No exception also. I've debug and try, but no luck. Any idea where I'm going wrong.

      Thanks a lot,
      ItsJava
        • 1. Re: Updating an XML attribute values.
          807591
          Where is the code that you are writing this XML:?
          • 2. Re: Updating an XML attribute values.
            807591
            I've not use a code to write the XML file. Just write it on the notepad and save it as a XML file.
            • 3. Re: Updating an XML attribute values.
              807591
              ItsJava wrote:
              I've not use a code to write the XML file. Just write it on the notepad and save it as a XML file.
              LOL, no, he's asking where do you write out your XML file from your program so that you can update the file. Just reading it in and changing the data in memory of course won't change the file, but you know that, right? You have to write it out to change it, and we have to see that code if we can fix it. So, can we see it?
              • 4. Re: Updating an XML attribute values.
                807591
                Sorry sir, that is the only code I try to use.
                public class XMLUpdates {
                    public static void main(String[] args) {
                        // TODO code application logic here
                        try{
                            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                            
                            Document doc = docBuilder.parse (new File("records.xml"));
                        
                            Node ss = doc.createElement("option");
                            ss.setTextContent("100");
                            doc.getFirstChild().appendChild(ss);
                        }
                        catch(SAXParseException ex){
                            System.out.println(ex.getLineNumber() + ex.getMessage());
                        }
                        catch(SAXException ex){
                            System.out.println(ex.getException());
                        }
                        catch(Throwable th){
                            th.printStackTrace();
                        }
                    }
                
                }
                You said that my code only read the file. If got the idea of the doc correctly appendChild() should update the value.

                I'm confusing is that not update any, or not given any error message. :(:(
                • 5. Re: Updating an XML attribute values.
                  807591
                  ItsJava wrote:
                  You said that my code only read the file. If got the idea of the doc correctly appendChild() should update the value.
                  Of the in memory doc object yes.

                  But not the stuff on disk. To update what's on disk you need to write the contents of your updated doc out.
                  • 6. Re: Updating an XML attribute values.
                    807591
                    edit: deleted as redundant.

                    Edited by: Encephalopathic on Apr 22, 2008 10:39 PM
                    • 7. Re: Updating an XML attribute values.
                      807591
                      I've search on the web for an example and no luck. :(

                      If you know such an example please can you put it here
                      • 8. Re: Updating an XML attribute values.
                        807591
                        [http://www.google.com/search?hl=en&q=Writing+XML+in+Java&meta=]
                        • 9. Re: Updating an XML attribute values.
                          807591
                          Thanks for the quick replay.

                          I'm always confusing on those some examples, that just use the println() and write as a xml file. :(

                          Need to read few of them and, if I stuck better to back again. :)
                          • 10. Re: Updating an XML attribute values.
                            807591
                            Ok, I read the xml file and get the required value as follows. Using DOM
                                    try{
                                        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                                        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                                        
                                        Document doc = docBuilder.parse (new File("records.xml"));
                                        
                                        NodeList ints = doc.getElementsByTagName("option");
                                        if(ints.getLength() != 0) {
                                            Element firstKeyElement = (Element)ints.item(0);
                                            NodeList firstKey = firstKeyElement.getChildNodes();
                                            
                                            String value = ((Node)firstKey.item(0)).getNodeValue().trim();
                                            int intValue = Integer.parseInt(value);
                                            System.out.println(intValue);
                                        }
                                    }
                                    catch(SAXParseException ex){
                                        System.out.println(ex.getLineNumber() + ex.getMessage());
                                    }
                                    catch(SAXException ex){
                                        System.out.println(ex.getException());
                                    }
                                    catch(Throwable th){
                                        th.printStackTrace();
                                    }
                            Is that the way I workout correct?

                            Now I want to change the intValue and write it into same place. :)
                            • 11. Re: Updating an XML attribute values.
                              807591
                              Ok, here what I tried up to now. With a new value and the Node called this function.
                                  private static void setValue(Node node, String value){
                                          if(node.getNodeType()!=Node.TEXT_NODE){
                                                  NodeList l=node.getChildNodes();
                                                  for(int i=0;i<l.getLength();i++){
                                                          if(l.item(i).getNodeType()==Node.TEXT_NODE){
                                                                  l.item(i).setNodeValue(value);
                                                                  return;
                                                          }
                                                          
                                                  }
                                          }
                                          else{
                                              try{
                                                  node.setTextContent(value);
                                                  System.out.println(value);
                                              }
                                              catch(DOMException e) {
                                                  System.out.println(e.getMessage() + " " + e.getLocalizedMessage());
                                              }
                                          }
                                  }
                              But this not work. Not given any exception at all. Debug and try, everything is ok. Actually the line System.out.println(value); give the answer too. But the above line on it not give any response. Debug shows that executing that line.

                              Can you guys tell me where I'm going wrong with?

                              Thanks.
                              • 12. Re: Updating an XML attribute values.
                                807591
                                ...
                                            Node node = doc.createTextNode("nothing");
                                
                                            setValue(node, "wibble");
                                
                                            if ("wibble".equals(node.getTextContent()))
                                                System.out.println("It works");
                                            else
                                                System.out.println("oops: " + node + " => " + node.getTextContent());
                                With the code you posted above, this prints out 'wibble' then 'It works'.

                                Can you give a simple test case where setValue(...) does print out the value passed in, 'But this not work'?