Results 1 to 5 of 5
Thread: [SOLVED] Update an XML file
- 04-29-2008, 05:43 AM #1
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
[SOLVED] Update an XML file
Hi all,
Here is my XML file format.
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.Java Code:<?xml version="1.0"?> <data> <key>467</key> <name>Paul</name> <id>123</id> </data>
I tried this.
Here is the setValue() method.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"); } }
But it not update the value. At the time I just try to write a string, without doing any modifications on the existing value.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()); } } }
Anyone of you can figure, where I'm going wrong. Debug and try but no any exceptions at all.
Eranga.
- 07-04-2008, 08:38 AM #2
Member
- Join Date
- Jul 2008
- Location
- india
- Posts
- 35
- Rep Power
- 0
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, 08:40 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 07-15-2008, 01:35 PM #4
Member
- Join Date
- Jul 2008
- Posts
- 1
- Rep Power
- 0
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
- 07-15-2008, 02:03 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Yes, as I said it's another approach pal. I've already solved this. Thanks for the comment.
Similar Threads
-
JPanel won't update
By ibanez270dx in forum New To JavaReplies: 3Last Post: 01-06-2009, 08:59 PM -
Update a record in Random access file
By Rgfirefly24 in forum New To JavaReplies: 2Last Post: 04-24-2008, 10:07 PM -
Using sql:update tag
By Java Tip in forum Java TipReplies: 0Last Post: 01-13-2008, 11:49 PM -
Java Update Installation
By guest in forum New To JavaReplies: 1Last Post: 12-01-2007, 04:36 AM -
dynamic update in swt
By sandor in forum SWT / JFaceReplies: 0Last Post: 05-14-2007, 08:32 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks