Results 1 to 4 of 4

Thread: Write an element to XML file

  1. #1

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Write an element to XML file

    Hi all,

    I have a xml file, the structure as follows.

    <?xml version="1.0"?>

    <data>
    <key>467</key>
    <name>eranga262154</name>
    </data>
    I want to write extra tag as follows
    <?xml version="1.0"?>

    <data>
    <key>467</key>
    <name>eranga262154</name>
    <id>123</id>
    </data>
    How can I do that. I'll try something like this, but it's not update my xml file.

    Code:
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                
                Document doc = docBuilder.parse (new File("records.xml"));
                
            // Try to add an additional node
                Node data = doc.getFirstChild();
                NamedNodeMap dataAttributes = data.getAttributes();
                Attr options = doc.createAttribute("id");
                options.setValue("123");
                dataAttributes.setNamedItem(options);
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Write an element to XML file

    You're code is not writing the values to the file
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Write an element to XML file

    Can you tell me where I'm going wrong.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

  4. #4

    Thread Starter
    PowerPoster eranga262154's Avatar
    Join Date
    Jun 2006
    Posts
    2,201

    Re: Write an element to XML file

    I start this in different way. My initial file is like this.

    Code:
     <?xml version="1.0"?>
    
    <data>
    <key>467</key>
    <name>eranga262154</name>
    <id>123</id>
    </data>
    I want to read the value of id and change it and write back. Here is the way I read the value.

    Code:
            try{
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                
                Document doc = docBuilder.parse (new File("records.xml"));
                
                NodeList ints = doc.getElementsByTagName("id");
                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(value);
                    
                // change the value and write it to the same location
    
                 }
            }
    How can I write value in this way. I tried setNodeValue() and it wont work. No compile or runtime error either.
    Last edited by eranga262154; Apr 24th, 2008 at 05:01 AM.
    “victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha

Posting Permissions

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



Click Here to Expand Forum to Full Width