?
Solved

load and parse xml

Posted on 2009-05-18
6
Medium Priority
?
286 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:Adwait Chitaley
[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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 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