This week's book giveaway is in the Java in General forum.
We're giving away four copies of Helidon Revealed: A Practical Guide to Oracle’s Microservices Framework and have Michael Redlich on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

unable to read value from XML file

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic