Solved

load and parse xml

Posted on 2009-05-18
6
280 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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

22 Experts available now in Live!

Get 1:1 Help Now