• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Need help in modifying XML text value using DOm parser

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Your mother is a hamster and your father smells of tiny ads!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic