|
|
|
|
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.
|
|

04-29-2008, 07:43 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
|
[SOLVED] Update an XML file
Hi all,
Here is my XML file format.
<?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.
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.
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.
|
|

07-04-2008, 10:38 AM
|
|
Member
|
|
Join Date: Jul 2008
Location: india
Posts: 29
|
|
|
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);
|
|

07-04-2008, 10:40 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
|
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.
|
|

07-15-2008, 03:35 PM
|
|
Member
|
|
Join Date: Jul 2008
Posts: 1
|
|
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. 
|
|

07-15-2008, 04:03 PM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 5,075
|
|
|
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.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
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