Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Pro XML Development with Java Technology 2006 phần 9 pps
Nội dung xem thử
Mô tả chi tiết
CHAPTER 11 ■ CONVERTING XML TO SPREADSHEET, AND VICE VERSA 307
case 8:
HSSFRow row8 = spreadsheet.getRow(8);
Element invincome1 = document.createElement("invincome");
stmtElement1.appendChild(invincome1);
invincome1.appendChild
(document.createTextNode
(row8.getCell((short) 1).
getStringCellValue()));
Element invincome2 = document.createElement("invincome");
stmtElement2.appendChild(invincome2);
invincome2.appendChild
(document.createTextNode
(row8.getCell((short) 2).
getStringCellValue()));
break;
case 9:
HSSFRow row9 = spreadsheet.getRow(9);
Element incbeforetaxes1 = document
.createElement("incbeforetaxes");
stmtElement1.appendChild(incbeforetaxes1);
incbeforetaxes1.appendChild
(document.createTextNode
(row9.getCell((short) 1).
getStringCellValue()));
Element incbeforetaxes2 =
document.createElement("incbeforetaxes");
stmtElement2.appendChild(incbeforetaxes2);
incbeforetaxes2.appendChild
(document.createTextNode
(row9.getCell((short)2).
getStringCellValue()));
break;
case 10:
HSSFRow row10 = spreadsheet.getRow(10);
Element taxes1 = document.createElement("taxes");
stmtElement1.appendChild(taxes1);
taxes1.appendChild(document.createTextNode(row10.getCell(
(short) 1).getStringCellValue()));
Element taxes2 = document.createElement("taxes");
stmtElement2.appendChild(taxes2);
Vohra_706-0C11.fm Page 307 Tuesday, August 8, 2006 6:43 AM
308 CHAPTER 11 ■ CONVERTING XML TO SPREADSHEET, AND VICE VERSA
taxes2.appendChild(document.createTextNode(row10.getCell(
(short) 2).getStringCellValue()));
break;
case 11:
HSSFRow row11 = spreadsheet.getRow(11);
Element netincome1 = document.createElement("netincome");
stmtElement1.appendChild(netincome1);
netincome1.appendChild(document.createTextNode(row11
.getCell((short) 1).getStringCellValue()));
Element netincome2 = document.createElement("netincome");
stmtElement2.appendChild(netincome2);
netincome2.appendChild(document.createTextNode(row11
.getCell((short) 2).getStringCellValue()));
break;
default:
break;
}
}
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
//Add indentation to output
transformer.setOutputProperty
(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
} catch (IOException e) {
System.out.println("IOException " + e.getMessage());
} catch (ParserConfigurationException e) {
System.out
.println("ParserConfigurationException " + e.getMessage());
} catch (TransformerConfigurationException e) {
System.out.println("TransformerConfigurationException "
+ e.getMessage());
} catch (TransformerException e) {
System.out.println("TransformerException " + e.getMessage());
}
}
Vohra_706-0C11.fm Page 308 Tuesday, August 8, 2006 6:43 AM
CHAPTER 11 ■ CONVERTING XML TO SPREADSHEET, AND VICE VERSA 309
public static void main(String[] argv) {
ExcelToXML excel = new ExcelToXML();
File input = new File("IncomeStatements.xls");
excel.generateXML(input);
}
}
You can run the application ExcelToXML.java in Eclipse with the procedure explained in
Chapter 1. Listing 11-16 shows the output from the ExcelToXML.java application.
Listing 11-16. Output from ExcelToXML.java
<?xml version="1.0" encoding="UTF-8"?>
<incmstmts>
<stmt>
<year>2005</year>
<revenue>11837</revenue>
<costofrevenue>2239</costofrevenue>
<researchdevelopment>1591</researchdevelopment>
<salesmarketing>2689</salesmarketing>
<generaladmin>661</generaladmin>
<totaloperexpenses>7180</totaloperexpenses>
<operincome>4657</operincome>
<invincome>480</invincome>
<incbeforetaxes>5137</incbeforetaxes>
<taxes>1484</taxes>
<netincome>3653</netincome>
</stmt>
<stmt>
<year>2004</year>
<revenue>10818</revenue>
<costofrevenue>1875</costofrevenue>
<researchdevelopment>1421</researchdevelopment>
<salesmarketing>2122</salesmarketing>
<generaladmin>651</generaladmin>
<totaloperexpenses>6069</totaloperexpenses>
<operincome>4749</operincome>
<invincome>420</invincome>
<incbeforetaxes>5169</incbeforetaxes>
<taxes>1706</taxes>
<netincome>3463</netincome>
</stmt>
</incmstmts>
Summary
The Apache POI API provides a useful mechanism for converting data between XML and spreadsheets.
In this chapter, you learned how to convert an example XML document to an Excel spreadsheet and
then convert the spreadsheet to an XML document. With XML being a universal format, there really
is no limit to what you can do with it!
Vohra_706-0C11.fm Page 309 Tuesday, August 8, 2006 6:43 AM
Vohra_706-0C11.fm Page 310 Tuesday, August 8, 2006 6:43 AM
311
■ ■ ■
CHAPTER 12
Converting XML to PDF
In the previous chapter, we discussed the procedure to convert an XML document to a Microsoft
Excel spreadsheet. In this chapter, we will show how to convert an XML document to a PDF document.
The open source Apache Formatting Objects Processor (FOP) project provides an API to convert an
XML document to PDF or other formats such as Printer Control Language (PCL), PostScript (PS),
Scalable Vector Graphics (SVG), XML, Print, Abstract Window Toolkit (AWT), Maker Interchange
Format (MIF), or TXT. You can also set the layout and font with the Apache FOP API. The Apache
FOP takes an XSL formatting object (an XSL-FO object) as input and produces a PDF (or other format)
document as output. XSL-FO is defined in the XSL 1.0 specification.1 Therefore, to convert XML to
PDF, you first need to convert XML to XSL-FO and subsequently convert XSL-FO to PDF.
Installing the Software
Before you can set up your project, you need to download the Apache FOP2 zip file fop-0.20.5-bin.zip
and extract the zip file to a directory. Assuming <FOP> is the directory in which you extracted the FOP
zip file, you need the JAR files listed in Table 12-1 for developing an XML to PDF conversion application.
You also need to download JDK 5.0. (You can also use another version of the JDK such as 1.4
or 6.0.)
1. See http://www.w3.org/TR/xsl/.
2. For more information about Apache FOP, see http://xmlgraphics.apache.org/fop/.
Table 12-1. Apache FOP JAR Files
JAR File Description
<FOP>/build/fop.jar FOP API classes
<FOP>/lib/avalon-framework-cvs-20020806.jar Logger classes
<FOP>/lib/batik.jar Graphics classes
<FOP>/lib/xercesImpl-2.2.1.jar The Xerces API
<FOP>/lib/xml-apis.jar The XML API
<FOP>/lib/xalan-2.4.1.jar The XSLT API
Vohra_706-0C12.fm Page 311 Saturday, July 29, 2006 6:20 AM
312 CHAPTER 12 ■ CONVERTING XML TO PDF
Setting Up the Eclipse Project
To compile and run the code examples, you will need an Eclipse project. You can download project
Chapter12 from the Apress website (http://www.apress.com) and import it into your Eclipse workspace by selecting File ➤ Import. The Chapter12 project consists of a com.apress.pdf package and
the Java class XMLToPDF.java in the package. The XMLToPDF.java application performs the XML to
PDF conversion. The Chapter12 project also consists of an example XML document (catalog.xml
in Listing 12-1) and an example XSLT style sheet (catalog.xslt in Listing 12-2).
To compile and run the XML to PDF code example, you need some Apache FOP JAR files in your
project’s Java build path; Figure 12-1 shows these JAR files. You also need to set the JRE system library
to JRE 5.0, as shown in Figure 12-1.
Figure 12-1. Chapter12 Java build path
Figure 12-2 shows the Chapter12 project directory structure.
Vohra_706-0C12.fm Page 312 Saturday, July 29, 2006 6:20 AM