This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Need help in modifying XML text value using DOm parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Need help in modifying XML text value using DOm parser " Watch "Need help in modifying XML text value using DOm parser " New topic
Author

Need help in modifying XML text value using DOm parser

Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Hi,
I have to insert text value in one of the child node of the XML file. I am using DOM parser. Can any one help me how to do that. I tried one function but its throwing class cast Exception. Dont know why...
Please help me as soon as possible.


SCJP1.5(81%), SCDJWS(94%), next mission SCEA (but need to wait or that)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
Which line is throwing the exception? What does the XML look like?

Have you done a "System.out.println(obj.getClass().getName()" on the object that you are trying to cast?


Ping & DNS - my free Android networking tools app
Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Element place = (Element) root.getFirstChild();
above line is throwing class cast exception

Let me explain my problem in detail--

above is my sample XML file in click node I have to insert text value and that too one text value in one node at a time.I am using DOM parser . how can I do that .
Please help me
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
getFirstChild returns a Node. Element is just one of several possible subclasses of Node; which one is returned in your case? (This is where the getClass().getName() call I mentioned earlier comes in handy.)
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Almost always the problem with getFirstChild() is that it is returning a white-space text node that the programmer didn't notice. I didn't look at the details but I notice your XML example does have a lot of white-space text nodes in it.
Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Hi,
While pasting the XML code I got white spaces otherwise I dont have white spaces in my code or you are considering below code as also white space(as there is space indent between Input and Button). Could you please tell me the what are the things I need to consider before parsing an XML file like check for white spaces etc...

please help me regarding this.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
Yes, that counts as whitespace text nodes. That's why I said you should ascertain what kind of Node subclass getFirstChild returns.

There's nothing much you need to consider beforehand; the code just needs to be aware that those nodes can be there.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It seems to me you want to get a Button element that is a child of an Input element. Is that correct? If so then I think you must have overlooked the getElementsByTagName method of the Element class.
Divya Gehlot
Ranch Hand

Joined: Sep 10, 2006
Posts: 243
Hi,
Before I was using Element class but it was throwing classcast exception so now I left that now I ma using Node and using methods getFirstChild(), getNextSibling() etc... but because of that my code becoming lengthy. Please suggest some solution.
and tell me where we should use Node and where Element as I am confused in that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Need help in modifying XML text value using DOm parser
 
Similar Threads
node type text
xml JAVA
Help parse using JAXP
Confusions about a DOM hierarchy
need tutorial on DOM parser on traversing Node element one at a time