Solved

load and parse xml

Posted on 2009-05-18
6
285 Views
Last Modified: 2012-05-07
i want to generate  report using jasper report with xml as datasource
i m getting the null value in doc of this line
  System.out.println("After Parse method_______________->++++++ "+doc);

my xml file is in reports folder of my project

and it gives the following exception  
i m sending the whole code
in code block i give the action class


package net.ensode.jasperbook.struts;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperRunManager;
 
import net.sf.jasperreports.engine.data.JRXmlDataSource;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Document;
public class GenerateReportAction extends Action
{
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
 
     ServletContext application=servlet.getServletContext();
     ServletOutputStream servletOutputStream =response.getOutputStream();
 
     try{
 
          
         System.out.println("Compiling report...");
         JasperCompileManager.compileReportToFile("C:/Documents and Settings/ashish/My Documents/NetBeansProjects/StrutsJasperCompleteApplication/reports/RemoteReport.jrxml");
         System.out.println("Compilation Succesfully----------------------->");
 
         BasicDom basicDom=new BasicDom();
         Document doc =basicDom.parseXmlFile("reports/userxml.xml", false);
         System.out.println("After Parse method_______________->++++++ "+doc);
         JRXmlDataSource xmlDataSource = new JRXmlDataSource(doc,"/userData/M55_USERS_NEW");
 
         System.out.println("Filling report...");
         InputStream reportStream=new FileInputStream("C:/Documents and Settings/ashish/My Documents/NetBeansProjects/StrutsJasperCompleteApplication/reports/RemoteReport.jasper");
         System.out.println("ReportStream---------->>>>>>>>>>>>>>>>"+reportStream.toString());
         System.out.println("Done!");
         JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream, new HashMap(),xmlDataSource);
          response.setContentType("application/pdf");
        }catch(Exception e){
            e.printStackTrace();
        }
     finally{
       
        servletOutputStream.flush();
        servletOutputStream.close();
     }
     return mapping.getInputForward();
    }
}
 
____________________________________________________________________
this is my  BasicDom.java 
 
 
 
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
 
package net.ensode.jasperbook.struts;
 
import java.io.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;
 
    public class BasicDom {
      
        // Parses an XML file and returns a DOM document.
        // If validating is true, the contents is validated against the DTD
        // specified in the file.
        public static Document parseXmlFile(String filename, boolean validating) {
            try {
                // Create a builder factory
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                factory.setValidating(validating);
                System.out.println("In Parse Methood++++++++++++++++++++++++++++++++++");
                // Create the builder and parse the file
                Document doc = factory.newDocumentBuilder().parse(new File(filename));
                return doc;
            } catch (SAXException e) {
                e.printStackTrace();
                // A parsing error occurred; the xml input is not valid
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

Open in new window

0
Comment
Question by:achitaley
[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
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24418979
how are you running it?


If you would answer some of my questions I can get it working for you ;)

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24418986

Document doc =basicDom.parseXmlFile(getClass().getResourceAsStream("/reports/userxml.xml", false));

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24418993
Typo sorry
:
 
	
 
Document doc =basicDom.parseXmlFile(getClass().getResourceAsStream("/reports/userxml.xml"), false);

Open in new window

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 86

Expert Comment

by:CEHJ
ID: 24419005
Sorry - i see it's file-based. Ignore that
0
 
LVL 92

Expert Comment

by:objects
ID: 24419147
> how are you running it?

If you are running it in a web application then use this:

ServletContext application = getServlet().getServletContext();
Document doc =basicDom.parseXmlFile(application.getRealPath("reports/userxml.xml"), false);

0
 
LVL 2

Accepted Solution

by:
d_techguy earned 500 total points
ID: 24419149
hummm i think this the problem with the path of file you are giving......

as i do not have you folder structure, the approach below is just a work around for you to find if that really is the problem. Its long and weird, but really helpful in determining the paths problem.
use the following code to create file in your code above.

File file = new File("abc.txt");
file.createNewFile();

this will create file somewhere in you project's directory, check if that is created parallel to "reports" folder? Most likely answer will be "No" and this is where your problem is. Now, use location of "abc.txt"  to determine the right path to your required XML.

cheers

0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…

691 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