Sunday, February 08, 2009

XSLT with Java and Dom4j-1.6

Feeling geeky again!!

Today I´ll show you how to perform an XSLT (XSL transformation) on an XML file using Java and Dom4j. In contrast with the last Java/Dom4j post in this i´ll only show the class that acually performs the transformation. Dom4j is not able to perform the transformation on its own, it needs the help of JAXP:



1 package com.julio.xml;
2
3 import java.io.File;

4 import java.io.FileWriter;
5 import java.io.IOException; // dom4j import
6 import org.dom4j.Document;
7 import org.dom4j.DocumentException;

8 import org.dom4j.io.SAXReader;
9 import org.xml.sax.ErrorHandler;
10 import org.xml.sax.SAXException;
11 import org.xml.sax.SAXParseException;

12
13 import javax.xml.transform.Transformer;
14 import javax.xml.transform.TransformerConfigurationException;
15 import javax.xml.transform.TransformerException;
16 import javax.xml.transform.TransformerFactory;

17 import javax.xml.transform.stream.StreamSource;
18
19 import org.dom4j.io.DocumentResult;
20 import org.dom4j.io.DocumentSource;
21 import org.dom4j.io.DocumentSource;

22 import org.dom4j.io.XMLWriter;
23 import org.dom4j.io.OutputFormat;
24
25 public class XmlTransformer {

26 // The input XML file
27 private File input;
28 // The DOM document for the imput file

29 private Document inputDoc;
30 // Guess what!!!!
31 private File output;

32 // The xslt file
33 private File transformation;
34
35 public XmlTransformer(File input, File transformation) {

36 this.input = input;
37 this.transformation = transformation;

38 }
39
40 public File transform() throws IOException {

41 // Need to create a factory using JAXP
42 TransformerFactory factory = TransformerFactory.newInstance();

43 try {
44 // Now get a transformer using the facory
45 Transformer transformer = factory.newTransformer(new StreamSource(

46 transformation));
47 // Convert the input file into a DOM document
48 this.createDoc();

49 // Dom4j support for trasnformations
50 DocumentSource source = new DocumentSource(this.inputDoc);

51 DocumentResult result = new DocumentResult();
52 try {

53 // OK lets do the actual transformation
54 transformer.transform(source, result);
55 // The rest is boiler plate code!!!

56 Document outputDoc = result.getDocument();
57 OutputFormat format = OutputFormat.createPrettyPrint();

58 output = new File("output.xml");
59 XMLWriter writer = new XMLWriter(new FileWriter(output), format);

60 writer.write(outputDoc);
61 writer.close();

62 } catch (TransformerException e) {
63 // TODO Auto-generated catch block

64 e.printStackTrace();
65 }
66 } catch (TransformerConfigurationException e) {

67 // TODO Auto-generated catch block
68 e.printStackTrace();
69 }

70 return output;
71 }
72
73 private void createDoc() {

74 SAXReader reader = new SAXReader(false);
75 try {

76 this.inputDoc = reader.read(this.input);

77 } catch (DocumentException e) {
78 // TODO Auto-generated catch block

79 e.printStackTrace();
80 }
81 }

82 }




syntax highlighted by Code2HTML, v. 0.9.1

Labels: , , ,

0 Comentarios:

Post a Comment

<< Home