Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

load and parse xml

Posted on 2009-05-18
6
Medium Priority
?
288 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
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!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

604 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