aspose file tools*
The moose likes Java in General and the fly likes Set the TextValue  for the xml tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Set the TextValue  for the xml tag" Watch "Set the TextValue  for the xml tag" New topic
Author

Set the TextValue for the xml tag

renu richard
Ranch Hand

Joined: Oct 06, 2008
Posts: 116
Hi,

My requirement is... i have an xml file. In which i need to set the value for a tag.



Please refer to the xml file .My requirement is , say for example...in the attached xml,

book tag is repeating. First i need to see which book is having the attribute 'id' as bk102. And then i need to find whether the value of title is "Midnight Rain". If its Midnight Rain then i need to update the value of "price" tag to 10. Currently it is 5.95..i need to change this to 10.

Can someone tell me which would be the best approach to do this.

Thanks !!!


Cheers,
Richard
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What have you tried so far? How are you parsing the XML?
renu richard
Ranch Hand

Joined: Oct 06, 2008
Posts: 116
I am using javax.xml.parsers.DocumentBuilder to read the xml file and using org.w3c.dom.Document for parsing the file. I could read the values of the tag , attributes using NodeList. But i am not sure how to set the value for the tag. I tried using setNodeValue for setting the value for the tag. But its not updating the value.

How can i set the value for the xml tag ??
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

Node has a method called setTextContent.

Alternatively you can create a org.w3c.dom.Text element by calling Document's createTextNode method. You can then call appendChild on the node to add it. You may want to remove all other children first:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12678
    
    5
Note that changing the DOM in memory does not automatically change the source document.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Set the TextValue for the xml tag
 
Similar Threads
Creation of GUI from xml file
Set the Text for the xml tag
Turn this Java into Groovy!
Newbie problem with XSLT
convert a db blob of xml into a jdom XML object