Solved

Sax parser XML Transformer Exception error in java

Posted on 2004-09-09
17
1,304 Views
Last Modified: 2013-11-23
I have used the method listed below called parseXmlFile() in a JSP page to parse XML in order to use XPath queries and it always ran fine.  Now, I am trying to run it in a java class...the application works great and reaches the class just fine, and everything compiles except for the XPath queries.  Below I have pasted a snippet of code comprising of part of a method called pricing() which calls parseXmlFile() which I pasted in its entirety.  Please forgive me and ignore and missed brackets if there are any, due to a bad pasting job.

The error I get is this:
"unreported exception java.xml.transform.TransformerException;must be caught or declared to be thrown" and the lines in question is anywhere where the query is being parsed with the object (called 'doc' in this case).  For example,  String calVarCheck = org.apache.xpath.XPathAPI.selectSingleNode(doc, optLength).getNodeValue();.

I tested the java class and it does in fact parse the query during runtime which means of course, it sees the xml document since when I changed the name of the xml document from Pricing.xml to something else, it said it couldn't find the xml, which is a good sign.  The problem is just with the 'doc' object, or at least I think.  If anyone can help it would be GREATLY APPRECIATED!  :)


import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import org.w3c.dom.Node;
import org.w3c.dom.Document;

pricing(udc,optionId);

public static Document parseXmlFile(String filename, boolean validating) {
            try {
                // Create a builder factory
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setValidating(validating);
 
                // Create the builder and parse the file
                Document doc = factory.newDocumentBuilder().parse(new File(filename));
                return doc;
            } catch (SAXException e) {
                System.out.println("A parsing error occurred; the xml input is not valid:" + e);
            } catch (ParserConfigurationException e) {
                System.out.println("ParserConfigurationException:" + e);
            } catch (IOException e) {
                System.out.println("IOException:" + e);
            }
            return null;
          }

public void pricing(String udc2, String optionId) {
     Document doc = parseXmlFile("\\Pricing.xml", false);
    HttpServletRequest request = null;
    HttpServletResponse response;
    HttpSession session = request.getSession();
   
    HashMap userSelection = (HashMap)session.getAttribute("userSelection");
   
    int currentPage = Integer.parseInt(request.getParameter("nextPage"))-1;
 
if(request.getParameter("OAL_UDC") != null){
            String lengthPull = request.getParameter("OAL_UDC");
            session.setAttribute("LENGTH", lengthPull);
 
 
                        String optLength = "//bom[@optid="+lengthPull+"]/@optQuant";

                    //This is an example of a Xpath line that is kicking back an error    
                        String calVarCheck = org.apache.xpath.XPathAPI.selectSingleNode(doc, optLength).getNodeValue();
                        }
}
 

 

 
0
Comment
Question by:gdlp2004
  • 6
  • 6
  • 3
  • +1
17 Comments
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 150 total points
ID: 12016686
>unreported exception java.xml.transform.TransformerException;must be caught or declared to be thrown

This exception is at compile time.
So put a try catch to manage this exception.
0
 
LVL 35

Accepted Solution

by:
TimYates earned 350 total points
ID: 12016718
From the API: http://xml.apache.org/xalan-j/apidocs/org/apache/xpath/XPathAPI.html#selectSingleNode(org.w3c.dom.Node,%20java.lang.String)

XPathAPI.selectSingleNode

throws a TransformerException

so change your code to:

  public void pricing(String udc2, String optionId) throws java.xml.transform.TransformerException  {

OR

  public void pricing(String udc2, String optionId) {
     Document doc = parseXmlFile("\\Pricing.xml", false);
    HttpServletRequest request = null;
    HttpServletResponse response;
    HttpSession session = request.getSession();
   
    HashMap userSelection = (HashMap)session.getAttribute("userSelection");
   
    int currentPage = Integer.parseInt(request.getParameter("nextPage"))-1;
 
if(request.getParameter("OAL_UDC") != null){
            String lengthPull = request.getParameter("OAL_UDC");
            session.setAttribute("LENGTH", lengthPull);
 
 
                        String optLength = "//bom[@optid="+lengthPull+"]/@optQuant";

                    //This is an example of a Xpath line that is kicking back an error    
                        try {
                            String calVarCheck = org.apache.xpath.XPathAPI.selectSingleNode(doc, optLength).getNodeValue();
                        }
                        catch( java.xml.transform.TransformerException ex ) {
                          ex.printStackTrace() ;
                        }
}
 
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12016719
>> "unreported exception java.xml.transform.TransformerException;must be caught or declared to be thrown" and the lines in question is

would be good to know that line.


To solve this error

1) or you have to place that line in a try catch block like this

     try {

     } catch (java.xml.transform.TransformerException ex) {
     }

2) If that line is in one of your functions, you have to declare the function as

     public yourFunc() throws java.xml.transform.TransformerException

 instead of

     public yourFunc()
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12016728
catching an exception doesn't mean that the code is wrong.
Some method over particular object (for example managing a file) rease an exception when some error occur.
So the message given to you tells only you must put into try{}catch (java.xml.transform.TransformerException ex){} the piece of code told to you.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12016736
Hi Tim :°D
0
 

Author Comment

by:gdlp2004
ID: 12016916
I think you all are right with this, so I tested it out, but when I tried renaming my method call to the way recommended by TimYates, it couldn't find the TransformerException package.  I searched on a java site and included this import as well in my code:

import javax.xml.transform.TransformerException;

and it still can't find this line that Tim wrote:

public void pricing(String udc2, String optionId) throws java.xml.transform.TransformerException

I know this is the right track and I appreciate everyone's help very much...if someone can help me get the right package in there I would be very grateful!
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12016940
this is the package and class
java.xml.transform.TransformerException
0
 

Author Comment

by:gdlp2004
ID: 12016947
I also tried putting the try catch around my selectSingleNode xpath query and it gets the same error...it can't find the TransformerException package.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 12

Expert Comment

by:Giant2
ID: 12016957
ops.

javax.xml.transform.TransformerException
0
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 150 total points
ID: 12016981
Use:
import javax.xml.transform.TransformerException;

and in the code put directly: TransformerException
0
 

Author Comment

by:gdlp2004
ID: 12016982
actually the package was correct, the public void pricing(String udc2, String optionId) throws java.xml.transform.TransformerException should be:

public void pricing(String udc2, String optionId) throws javax.xml.transform.TransformerException

simple fix...but thank you all for the help!!!  It works like a charm now. :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12017010
>> It works like a charm now. :)
Nice to hear that :)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12017019
Since me and Tim posted at exactly the same time I'm rather disappointed about the point split :(
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12017030
Thanks.
Happy to help you.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12017078
>> thank you ***all*** for the help!!!
If you forgot me by accident, this is how to correct:

1) Ask to reopen this question by posting a zero-point question in http://www.experts-exchange.com/Community_Support/

Subject: Moderator Please Reopen
Body:
Please reopen this question:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21124606.html

2) Once reopened by an admin (at most within 2 days) you get an e-mail
3) Then you can redo the split
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12051730
Hi gdlp2004, any comment?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now