Java Tutorial/XML/Transformer
Содержание
- 1 And now to attach an XSL
- 2 Applying XSLT Stylesheets
- 3 Catch TransformerException
- 4 Create an XML file and attach an XSL
- 5 Formatting an XML file using Transformer
- 6 Source for the JAXP Transformation Application
- 7 Source for Transforming DOM Node to HTML with JAXP
- 8 Transforming an XML File with XSL into a DOM Document
And now to attach an XSL
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class Main {
public static void main(String args[]) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
Document xmldoc = impl.createDocument(null, "TODOs", null);
Element root = xmldoc.getDocumentElement();
Element e0 = xmldoc.createElement("TOPIC");
Element e1 = xmldoc.createElement("TITLE");
Node n1 = xmldoc.createTextNode("Java");
e1.appendChild(n1);
Element e2 = xmldoc.createElement("URL");
Node n2 = xmldoc.createTextNode("http://www.server.ru");
e2.appendChild(n2);
e0.appendChild(e1);
e0.appendChild(e2);
root.appendChild(e0);
Node pi = xmldoc.createProcessingInstruction("xml-stylesheet",
"type=\"text/xsl\" href=\"TODOs.xsl\"");
xmldoc.insertBefore(pi, root);
StreamResult out = new StreamResult("howto.xml");
DOMSource domSource = new DOMSource(xmldoc);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(domSource, out);
}
}
Applying XSLT Stylesheets
import java.io.File;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class Main {
public static void main(String[] args) throws Exception{
StreamSource xmlFile = new StreamSource(new File(args[0]));
StreamSource xsltFile = new StreamSource(new File(args[1]));
TransformerFactory xsltFactory = TransformerFactory.newInstance();
Transformer transformer = xsltFactory.newTransformer(xsltFile);
StreamResult resultStream = new StreamResult(System.out);
transformer.transform(xmlFile, resultStream);
}
}
Catch TransformerException
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.SourceLocator;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class Main {
public static void xsl(String inFilename, String outFilename, String xslFilename) {
try {
TransformerFactory factory = TransformerFactory.newInstance();
Templates template = factory.newTemplates(new StreamSource(new FileInputStream(xslFilename)));
Transformer xformer = template.newTransformer();
Source source = new StreamSource(new FileInputStream(inFilename));
Result result = new StreamResult(new FileOutputStream(outFilename));
xformer.transform(source, result);
} catch (FileNotFoundException e) {
} catch (TransformerConfigurationException e) {
} catch (TransformerException e) {
SourceLocator locator = e.getLocator();
int col = locator.getColumnNumber();
int line = locator.getLineNumber();
String publicId = locator.getPublicId();
String systemId = locator.getSystemId();
}
}
}
Create an XML file and attach an XSL
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
public class Main {
public static void main(String args[]) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
Document xmldoc = impl.createDocument(null, "XMLDoc", null);
Element root = xmldoc.getDocumentElement();
Element e0 = xmldoc.createElement("Doc");
Element e1 = xmldoc.createElement("TITLE");
Node n1 = xmldoc.createTextNode("Java");
e1.appendChild(n1);
Element e2 = xmldoc.createElement("data");
Node n2 = xmldoc.createTextNode("text node");
e2.appendChild(n2);
e0.appendChild(e1);
e0.appendChild(e2);
root.appendChild(e0);
StreamResult out = new StreamResult("howto.xml");
DOMSource domSource = new DOMSource(xmldoc);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(domSource, out);
}
}
Formatting an XML file using Transformer
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class Main {
public static void main(String[] argv) throws Exception {
}
static void formatXMLFile(String file) throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new InputStreamReader(new FileInputStream(
file))));
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.setOutputProperty(OutputKeys.METHOD, "xml");
xformer.setOutputProperty(OutputKeys.INDENT, "yes");
xformer.setOutputProperty("b;http://xml.apache.org/xsltd;indent-amount", "4");
xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
Source source = new DOMSource(document);
Result result = new StreamResult(new File(file));
xformer.transform(source, result);
}
}
Source for the JAXP Transformation Application
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
public class JAXPTransform {
public static void main(String args[]) throws TransformerConfigurationException,
TransformerException, FileNotFoundException {
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource stylesheet = new StreamSource(args[1]);
StreamSource xmlDoc = new StreamSource(args[0]);
StreamResult result = new StreamResult(new FileOutputStream(args[2]));
Transformer transFormer = factory.newTransformer(stylesheet);
transFormer.transform(xmlDoc, result);
}
}
Source for Transforming DOM Node to HTML with JAXP
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
public class JAXPTransformNode {
public static void main(String args[]) throws Exception, TransformerException,
FileNotFoundException {
TransformerFactory factory = TransformerFactory.newInstance();
DOMSource stylesheet = new DOMSource(buildDoc(args[1]));
StreamSource xmlDoc = new StreamSource(args[0]);
StreamResult result = new StreamResult(new FileOutputStream(args[2]));
Transformer transFormer = factory.newTransformer(stylesheet);
transFormer.transform(xmlDoc, result);
}
public static Document buildDoc(String document) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document theDocument = db.parse(new File(document));
return theDocument;
}
}
Transforming an XML File with XSL into a DOM Document
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
public class Main {
public static void main(String[] argv) throws Exception {
TransformerFactory factory = TransformerFactory.newInstance();
Templates template = factory.newTemplates(new StreamSource(new FileInputStream("xsl.xlt")));
Transformer xformer = template.newTransformer();
Source source = new StreamSource(new FileInputStream("in.xml"));
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Result result = new DOMResult(doc);
xformer.transform(source, result);
}
}