List of usage examples for javax.xml.transform TransformerFactory newInstance
public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);/*w ww .j ava 2 s. c o m*/ factory.setXIncludeAware(true); DocumentBuilder parser = factory.newDocumentBuilder(); System.out.println("aware: " + parser.isXIncludeAware()); Document document = parser.parse(args[0]); Transformer transformer = TransformerFactory.newInstance().newTransformer(); Source source = new DOMSource(document); Result output = new StreamResult(System.out); transformer.transform(source, output); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(new File("test1.xml")); removeEmptyChildElements(doc.getDocumentElement()); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File("clean.xml")); transformer.transform(source, result); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<company><year id='2000'><quarter id='1' sales='80'/></year><year id='2001'><quarter id='1' sales='20'/></year></company>"; String xpath = "/company/year[@id=2001]"; XPath xPath = XPathFactory.newInstance().newXPath(); Node node = (Node) xPath.evaluate(xpath, new InputSource(new StringReader(xml)), XPathConstants.NODE); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(node), new StreamResult(System.out)); }
From source file:Main.java
public static void main(String[] args) throws Exception { String xmlSample = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><tag><nested>hello</nested></tag>"; String stylesheet = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><xsl:output method=\"xml\" version=\"1.0\" indent=\"yes\"/><xsl:template match=\"node()|@*\"><xsl:copy><xsl:apply-templates select=\"node()|@*\"/></xsl:copy></xsl:template></xsl:stylesheet>"; TransformerFactory factory = TransformerFactory.newInstance(); Source xslSource = new StreamSource(new StringReader(stylesheet)); Transformer transformer = factory.newTransformer(xslSource); Source source = new StreamSource(new StringReader(xmlSample)); Result result = new StreamResult(System.out); transformer.transform(source, result); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true);//from w ww.ja v a 2 s . co m DocumentBuilder db = dbf.newDocumentBuilder(); Document document = db.newDocument(); Element rootElement = document.createElement("root"); document.appendChild(rootElement); rootElement.setAttributeNS(XMLConstants.XML_NS_URI, "space", "preserve"); TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.transform(new DOMSource(document), new StreamResult(System.out)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { InputSource in = new InputSource(new FileInputStream("y.xml")); DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); dfactory.setNamespaceAware(true);/*from ww w. j av a 2 s .co m*/ Document doc = dfactory.newDocumentBuilder().parse(in); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); CachedXPathAPI path = new CachedXPathAPI(); NodeIterator nl = path.selectNodeIterator(doc, "\\abc\\"); Node n; while ((n = nl.nextNode()) != null) transformer.transform(new DOMSource(n), new StreamResult(new OutputStreamWriter(System.out))); }
From source file:Main.java
public static void main(String args[]) throws Exception { File stylesheet = new File("style.xsl"); File xmlSource = new File("data.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(xmlSource); StreamSource stylesource = new StreamSource(stylesheet); Transformer transformer = TransformerFactory.newInstance().newTransformer(stylesource); Source source = new DOMSource(document); Result outputTarget = new StreamResult(new File("/tmp/x.csv")); transformer.transform(source, outputTarget); }
From source file:Main.java
public static void main(String[] argv) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder//from w w w.j a va 2 s . com .parse(new InputSource(new InputStreamReader(new FileInputStream("inputFile.xml")))); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.METHOD, "xml"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("http://xml.apache.org/xslt;indent-amount", "4"); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); Source source = new DOMSource(document); Result result = new StreamResult(new File("result.xml")); xformer.transform(source, result); }
From source file:Main.java
public static void main(String[] args) throws Exception { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = docBuilderFactory.newDocumentBuilder(); Document node = documentBuilder.parse(new FileInputStream("data.xml")); cleanEmptyTextNodes(node);//w w w . j a va2s . c o m StreamResult result = new StreamResult(new StringWriter()); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(4)); transformer.transform(new DOMSource(node), result); System.out.println(result.getWriter().toString()); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = parser.parse(new InputSource(new StringReader(xmlString))); Transformer transformer = TransformerFactory.newInstance().newTransformer(); Source source = new DOMSource(document); Result output = new StreamResult(System.out); transformer.transform(source, output); }