?
Solved

Sax parser XML Transformer Exception error in java

Posted on 2004-09-09
17
Medium Priority
?
1,383 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 3
  • +1
17 Comments
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 600 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 1400 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 12

Expert Comment

by:Giant2
ID: 12016957
ops.

javax.xml.transform.TransformerException
0
 
LVL 12

Assisted Solution

by:Giant2
Giant2 earned 600 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 5 hours left to enroll

752 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