*
The moose likes XML and Related Technologies and the fly likes unable to read value from XML file 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 "unable to read value from XML file" Watch "unable to read value from XML file" New topic
Author

unable to read value from XML file

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
<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


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
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.


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
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

Joined: Mar 22, 2000
Posts: 12761
    
    5
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

Joined: Jun 20, 2002
Posts: 442
Thank you william.I figured it out and got the solution.I knew I was doing some silly mistake.Thanks for the info.
 
 
subject: unable to read value from XML file
 
Similar Threads
DTD not well formed
Recreate XML file
ID and IDREF attributes in XML
Hibernate : generated attribute of the property element
PCDATA and CDATA