Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-29-2008, 07:43 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
[SOLVED] Update an XML file
Hi all,

Here is my XML file format.

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.

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.

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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-04-2008, 10:38 AM
Member
 
Join Date: Jul 2008
Location: india
Posts: 29
pankaj_salwan is on a distinguished road
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);
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-04-2008, 10:40 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-15-2008, 03:35 PM
Member
 
Join Date: Jul 2008
Posts: 1
Amrutha is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-15-2008, 04:03 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Yes, as I said it's another approach pal. I've already solved this. Thanks for the comment.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Someone helped you?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.
Help:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Resources:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Web:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Tips:
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
JPanel won't update ibanez270dx New To Java 3 01-06-2009 10:59 PM
Update a record in Random access file Rgfirefly24 New To Java 2 04-25-2008 12:07 AM
Using sql:update tag Java Tip Java Tips 0 01-14-2008 01:49 AM
Java Update Installation guest New To Java 1 12-01-2007 06:36 AM
dynamic update in swt sandor SWT / JFace 0 05-14-2007 10:32 PM


All times are GMT +3. The time now is 12:00 PM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org