Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unable to read value from XML file

 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<TABLES>
<TABLE name="GIFT_CERTIFICATE_TYPE">
<COLUMN>
GC_TYPE
</COLUMN>
</TABLE>
</TABLES>


This is my XML file.I am trying to print value of tag <COLUMN> which is GC_TYPE with following code.

NodeList nodeList = tableNode.getElementsByTagName("COLUMN");

for(int i=0;i<nodeList.getLength();i++){
Node node1 = nodeList.item(i);
//String str = ((Text)node1).getData().trim();
System.out.println("NodeType " + node1.getNodeType());
String str = node1.getNodeValue();
System.out.println("Column1 "+str);
}

It is displaying
NodeType 1
Column1 null

Why is it ot displaying right value of <COLUMN>?Am I missing something?

Here is dtd

<?xml version="1.0" encoding="UTF-8"?>
<!--DTD generated by XMLSPY v2004 rel. 4 U (http://www.xmlspy.com)-->
<!ELEMENT TABLES (TABLE*)>
<!ELEMENT TABLE (COLUMN*)>
<!ATTLIST TABLE
name ID #REQUIRED
>

<!ELEMENT COLUMN (#PCDATA)>
<!ATTLIST COLUMN
ID ID #REQUIRED
>

I am a beginer.Trying to learn XML...

Thanks
Veena
 
Nathaniel Stoddard
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does the COLUMN node have any children nodes? If it has one, what would the value and type of that node be? Try it out and see what happens.
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in API I saw node types defined as constants,but couldn't make out what are there values,where can I find values for them?And I need to know where I am wrong in my code?Coz I am thinking on this from long time ,.....everything looks straight forward.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13062
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a common mistake. The value you want is in the child node of <COLUMN>
<COLUMN> is an Element node - the nodeValue() of an Element node is always null - the Element has a single child node of type Node.TEXT_NODE
There is an excellent table in the org.w3c.dom.Node JavaDocs that summarizes all of the node types. SO -
String val = node1.getFirstChild().getNodeValue() ;
You might want to .trim() the resulting String since it will start with crlf and end with crlf.
Bill
 
Veena Pointi
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you william.I figured it out and got the solution.I knew I was doing some silly mistake.Thanks for the info.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic