aspose file tools*
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 Spring in Action this week in the Spring 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: 18658
    
    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.
 
Consider Paul's rocket mass heater.
 
subject: How to replace TextNode in XML?