• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

HSSF

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!

I am having a bit of problem here regarding HSSF. I am supposed to write a program that retrieves data from XML and output to excel using HSSF (as requested by my supervisor). My program can be compile with warnings, but it gave runtime error.

Exception in thread "main" java.lang.ClassCastException: org.apache.xml.dtm.ref.DTMNodeList cannot be cast to com.sun.org.apache.xml.internal.dtm.ref.DTMNodeList
at XMLToExcel.generateExcel(XMLToExcel.java:48)
at XMLToExcel.main(XMLToExcel.java:102)

Is there anything that I missed that caused this error?





Another thing would like to ask. My program will have to ask for user input and retrieve the data based on the input. I am using a if loop with GetElementByTag, but it seems to be give me commpile time error.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

According to the javadocs, XPath.evaluate for a NODESET returns a "org.w3c.dom.NodeList", so that's what you should cats it to. Casting to anything else means relying on implementation details, and is liable to cause all sorts of problems as you have found out.


I am using a if loop with GetElementByTag, but it seems to be give me commpile time error.


What error? In which line does it occur?
[ May 26, 2007: Message edited by: Ulf Dittmer ]
 
liew deb
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

Thank you for your fast reply.

That helps me real lot. Thank you.

The problem seems to lie with the if statement, if(getElementByTagName("id").item(0).getFirstChild().getNodeValue() == query). If I were to comment it, the program will compile smoothly with no errors. However, if I were to un-comment it, the error will be thrown to me.

XMLToExcel.java:60: cannot find symbol
symbol : method getElementByTagName(java.lang.String)
location: class XMLToExcel
if(getElementByTagName("id").item(0).getFirstChild().getNodeValue() == query){
^
 
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
getElementByTagName("id").item(0).getFirstChild().getNodeValue() == query)

getElementsByTagName is a method in the Element class - where is your Element reference?

getFirstChild() is VERY dangerous unless you are absolutely sure that the XML document is formatted without extra text elements.

General comment:
Great long constructions like that may look cool but they can hide any number of problems and create debugging chaos. I would prefer to plod through that logic step by step - confirming that the NodeList returned by getElementsByTagName is not empty before executing item(0) etc etc.

Bill
 
liew deb
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm... It seems that I can't use getFirstChild as the data is not converted to xls as expected. What would you suggest me to use instead of getFirstChild? Part of the XML file is as below. My apology if I am asking too much as I have limited time to understand HSSF and I am supposed to come up with the program tomorrow. =[

Thank you.
 
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

It seems that I can't use getFirstChild as the data is not converted to xls as expected.



I'm not sure what the premise is in this sentence, and what the conclusion.

If you are inferring that you can't use getFirstChild because you're not receiving the XLS file you'd like, then follow Williams suggestions and make sure that it really returns what you think it should be returning.

If you are inferring that you can't create an Excel file because you can't use getFirstChild, then that's not the proper conclusion. It merely means you may have to use other DOM methods. Again, as William suggests, make sure that at each intermediate step you're working with exactly the data you think you're working with. Lots of debugging output will help with this.
 
liew deb
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your help. I have another question.

XPathFactory factory =
XPathFactory.newInstance();
XPath xPath = factory.newXPath();
String expression = "/id/SQL";
org.w3c.dom.NodeList
nodeList =( org.w3c.dom.NodeList)
xPath.evaluate(expression,
inputSource, XPathConstants.NODESET);


is this the way to write to provide the evaluate methods to evaluate compiled XPath expressions in an XML document based on my XML given above? I suspect I am not getting the result due to the String expression. I might have done it wrongly.
 
Sheriff
Posts: 28328
96
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
That's the right code in general to evaluate an XPath expression and apply it to a document. It selects all "SQL" elements that are children of an "id" element that is a child of the context node.

And yes, that will not select anything from the XML document you posted earlier (it doesn't contain any "id" elements, for one thing). Was that your question?
 
liew deb
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm... Thank you for your reply...

I am thinking how should I retrieve the data for



Should it be

or

or

I tried all out, but it seem not to work as it keeps throwing nullPointerException.

(Edited to be not so extremely wide - PC)
[ May 28, 2007: Message edited by: Paul Clapham ]
 
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
The only part that differs in these 3 code excerpts (the String assignment) can't throw NullPointerExceptions, so I'm thinking that the problem you're facing right now lies somewhere else.
 
liew deb
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm... Is it ok if I post my code here and help me check where is the error?



Thank you.
 
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
You still have this statement that -as was pointed out before- is most likely a problem:



Take my advice and print out the intermediate values to make sure that you're retrieving what you think you're retrieving.

Also, if you're getting exceptions, tell us in which line they occur. If they occur in the line quoted above, break it into its pieces and indicate which object or method call causes it.
 
liew deb
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmm... I ass in these codes...



The problem seems to lie with the Element root = doc.getDocumentElement(). That was where the exception was thrown.
 
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
Then "doc" must be null, since you're getting a NullPointerException, correct?
 
It's fun to be me, and still legal in 9 states! Wanna see my 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