Results 1 to 5 of 5
  1. #1
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Wink [SOLVED] Update an XML file

    Hi all,

    Here is my XML file format.

    Java Code:
     <?xml version="1.0"?>
    
    <data>
    <key>467</key>
    <name>Paul</name>
    <id>123</id>
    </data>
    Using DOM, read the xml file and get the value of id. Then I want to do some modification on that value and write back/or update the existing value into new one.

    I tried this.

    Java Code:
            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();
                    System.out.println(value);
                    
                // change the value and write it to the same location
                    setValue((Node)firstKey.item(0), "ssss");
                 }
            }
    Here is the setValue() method.

    Java Code:
        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);
                    }
                    catch(DOMException e) {
                        System.out.println(e.getMessage() + " " + e.getLocalizedMessage());
                    }
                }
        }
    But it not update the value. At the time I just try to write a string, without doing any modifications on the existing value.

    Anyone of you can figure, where I'm going wrong. Debug and try but no any exceptions at all.

    Eranga.

  2. #2
    pankaj_salwan is offline Member
    Join Date
    Jul 2008
    Location
    india
    Posts
    35
    Rep Power
    0

    Default

    i did that... use SetTextCOntent anf then write dom to a file
    as follows:--
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    //StreamResult result = new StreamResult(System.out);
    StreamResult result = new StreamResult("out.txt");
    transformer.transform(source, result);

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    That's a different approach. I feel it's not easy to me, still not able to fix it. So that why I suggested a different way to you.

  4. #4
    Amrutha is offline Member
    Join Date
    Jul 2008
    Posts
    1
    Rep Power
    0

    Red face

    Eranga,
    I think what pankaj posted is correct. In ur code ur modified the value but ur not writing the modified content back into file. Thats what "Transformer" will do for u.
    U can check "java.sun.com/j2se/1.4.2/docs/api/javax/xml/transform/Transformer.html" for more info. :o

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. JPanel won't update
    By ibanez270dx in forum New To Java
    Replies: 3
    Last Post: 01-06-2009, 08:59 PM
  2. Update a record in Random access file
    By Rgfirefly24 in forum New To Java
    Replies: 2
    Last Post: 04-24-2008, 10:07 PM
  3. Using sql:update tag
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-13-2008, 11:49 PM
  4. Java Update Installation
    By guest in forum New To Java
    Replies: 1
    Last Post: 12-01-2007, 04:36 AM
  5. dynamic update in swt
    By sandor in forum SWT / JFace
    Replies: 0
    Last Post: 05-14-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •