XML to PDF conversion in core java

conversion XML file to PDF file in core java

Page 1 of 1

1 Replies - 12232 Views - Last Post: 01 December 2010 - 05:01 AM Rate Topic: -----

#1 kirti.adsul   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 26
  • Joined: 02-November 10

XML to PDF conversion in core java

Posted 01 December 2010 - 02:22 AM

I want to convert xml file to pdf file but which gives error like root element not found.
I have make xml_file.xml file
Please guid me as early as possible.

Code is as follow:

package xml_To_Pdf;
import org.apache.fop.apps.*;
import org.apache.xerces.parsers.SAXParser;
import java.io.*;
import org.apache.avalon.framework.logger.ConsoleLogger;
import org.apache.avalon.framework.logger.Logger;
import org.apache.fop.apps.FOPException;
import org.xml.sax.InputSource;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
public class xml_to_Pdf_file {

	 * @param args
	public static void main(String[] args) {
		xml_to_Pdf_file fop=new xml_to_Pdf_file();

	public void xmlToFO(){
		DocumentBuilderFactory factory =DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		File stylesheet = new File("file_folder\\xml_file.xsl");
		File xmlFile = new File("file_folder\\xml_file.xml");
		Document document = builder.parse(xmlFile);
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		Transformer transformer = transformerFactory.newTransformer(new StreamSource(stylesheet));
		DOMSource source = new DOMSource(document);
		StreamResult result = new StreamResult(new File("file_folder\\xml_file.fo"));
		transformer.transform(source, result);
		}catch(TransformerConfigurationException e)
		{System.err.println("TransformerConfigurationException: "+e.getMessage());}
		catch(TransformerException e)
		{System.err.println("TransformerException: "+e.getMessage());}
		catch(ParserConfigurationException e)
		{System.err.println("TransformerConfigurationException: "+e.getMessage());}
		catch(IOException e){System.err.println("TransformerException: "+e.getMessage());}
		catch(SAXException e){System.err.println("TransformerException: "+e.getMessage());}
	public void foToPDF(){
		Driver driver=new Driver();
		Logger logger=new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
		File xslFOFile=new File("file_folder\\xml_file.fo");
		File pdfFile=new File("file_folder\\xml_file.pdf");
		InputStream input=new FileInputStream(xslFOFile);
		driver.setInputSource(new InputSource(input));
		OutputStream output=new FileOutputStream(pdfFile);
		}catch(IOException e){System.err.println("IOException:" +e.getMessage());}
		catch(FOPException e){System.err.println("FOPException: "+e.getMessage());}


Please :code:

This post has been edited by pbl: 01 December 2010 - 09:42 PM
Reason for edit:: code tags added

Is This A Good Question/Topic? 0
  • +

Replies To: XML to PDF conversion in core java

#2 m-e-g-a-z   User is offline

  • Winning
  • member icon

Reputation: 497
  • View blog
  • Posts: 1,457
  • Joined: 19-October 09

Re: XML to PDF conversion in core java

Posted 01 December 2010 - 05:01 AM

How does your XML file look like? Copy and post the error message generated.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1