Solved

load and parse xml

Posted on 2009-05-18
6
277 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
  • 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

747 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

11 Experts available now in Live!

Get 1:1 Help Now