Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help in modifying XML text value using DOm parser

 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20955
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic