Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dom (getNodeValue)

 
Liza Boa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
how can I get the content of a tag (for ex. the String "name")
<login>name</login>
? I have the tags stored in a NodeList "loginList", built with
document.getElementsByTagName("login").
While
loginList.item(int i).getNodeName() works fine and returns "login",
loginList.item(inti).getNodeValue() returns null.
Can somebody help me?
 
Kapil Sabharwal
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liza
I think the problem with your approach is that you are not accesing the Node.TEXT_NODE here, instead u are using Node.ELEMENT_NODE which in this case is <login>. Try this out when u get the NodeList "loginList":
NodeList child = loginList.item(0).getChildNodes();
//this is going to be the TEXT Node
Node text = child.item(0);
String xyz = text.getNodeValue();
Kapil

Originally posted by Liza Boa:
hi
how can I get the content of a tag (for ex. the String "name")
<login>name</login>
? I have the tags stored in a NodeList "loginList", built with
document.getElementsByTagName("login").
While
loginList.item(int i).getNodeName() works fine and returns "login",
loginList.item(inti).getNodeValue() returns null.
Can somebody help me?

 
Liza Boa
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Kapil!
best regards, Liza
 
Palani kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you have to get the value for "name"
you can do so like this
document.getElementByTagName("login").item(0).getFirstChild().getNodeValue();
Cheers
Palanikumar
 
Palani kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you have to get the value for "name"
you can do so like this
document.getElementByTagName("login").item(0).getFirstChild().getNodeValue();
Cheers
Palanikumar
------------------
In the end everything is Right,If it's not then It's not the End
 
Will Rogers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was having the same problem (getNodeValue returning null), so I tried the
nodelist.item(0).getFirstChild().getNodeValue()
method and now it's throwing NullPointerExceptions
my xml struct looks like this:
<root>
...<element attr="true">
......<subelement>data</subelement>
......<subelement2>data</subelement2>
...</element>
...<element attr="true">
......<subelement>data</subelement>
......<subelement2>data</subelement2>
...</element>
...<differentelement attr="spam">
......<subelement>data</subelement>
...</differentelement>
</root>
the tags have been changed to protect the innocent. . can anybody help with this?
the net of what I'm doing is this:
doc.getElementsByTagName().item(0).getChildNodes().item(0).getFirstChild().getNodeValue()
Am I digging too deep in this case?
thanks for your help
 
TimB
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I am having a simmilar issue, I have a Node:
<MS attribute="bla bla bla">This is my message</ms>
I am trying to get both the attribute and the value of the node. I have no problems getting the attributes the problem I am having is getting the node value. I am using:
message.getNodeValue();
Where the node name is message. This is giveing me a null value back. Any suggestions?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic