This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes How to replace TextNode in XML? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "How to replace TextNode in XML?" Watch "How to replace TextNode in XML?" New topic
Author

How to replace TextNode in XML?

Stan Shah
Greenhorn

Joined: Jul 27, 2006
Posts: 2
Hi,

I have a requirement where I need to create 2 XMLs with only one Element value (TextNode) being different and put on different MQ queues. I have already created one XML and put on a queue. Now how do I replace the Element value within that DOM Object.

E.g
--XML 1
<EMP>
<NAME>ABC</NAME>
<AGE>22</AGE>
</EMP>

Now how do I make this into
--XML 2
<EMP>
<NAME>XYZ</NAME>
<AGE>22</AGE>
</EMP>

I have org.w3c.dom.Document object holding the XML Message.

Thanks,
Stan
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You'll find the problem easier when you realize the non-obvious way that DOM works. Your question ("How do I replace the Element value") is meaningless because an Element doesn't have a value. But what you want to modify is actually the Text node that is the Element's only child.

So navigate to the <NAME> element, then to its first child, which should be a Text node. Call setValue("XYZ") on that node and you're done.
Stan Shah
Greenhorn

Joined: Jul 27, 2006
Posts: 2
Thanks it worked.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to replace TextNode in XML?
 
Similar Threads
Replace an element value in one xml file with value from another xml
Append Child node in DOM
problem in getting xpath of each node.
how to remove particular child element
Merging 2 xml files