This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes HSSF Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "HSSF" Watch "HSSF" New topic
Author

HSSF

liew deb
Greenhorn

Joined: May 22, 2007
Posts: 9
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.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42030
    
  64

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 ]

Ping & DNS - my free Android networking tools app
liew deb
Greenhorn

Joined: May 22, 2007
Posts: 9
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){
^
William Brogden
Author and all-around good cowpoke
Rancher

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

Joined: May 22, 2007
Posts: 9
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
Marshal

Joined: Mar 22, 2005
Posts: 42030
    
  64
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

Joined: May 22, 2007
Posts: 9
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: May 22, 2007
Posts: 9
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
Marshal

Joined: Mar 22, 2005
Posts: 42030
    
  64
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

Joined: May 22, 2007
Posts: 9
Hmm... Is it ok if I post my code here and help me check where is the error?



Thank you.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42030
    
  64
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

Joined: May 22, 2007
Posts: 9
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
Marshal

Joined: Mar 22, 2005
Posts: 42030
    
  64
Then "doc" must be null, since you're getting a NullPointerException, correct?
 
Consider Paul's rocket mass heater.
 
subject: HSSF