Example usage for javax.xml.transform Transformer transform

List of usage examples for javax.xml.transform Transformer transform

Introduction

In this page you can find the example usage for javax.xml.transform Transformer transform.

Prototype

public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;

Source Link

Document

Transform the XML Source to a Result.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("test.xml");
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();

    Document oldDoc = builder.parse(is);
    Node oldRoot = oldDoc.getDocumentElement();
    Document newDoc = builder.newDocument();
    Element newRoot = newDoc.createElement("newroot");
    newDoc.appendChild(newRoot);//from  w w w  . j a v  a  2  s  .  c  o m
    newRoot.appendChild(newDoc.importNode(oldRoot, true));

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    DOMSource domSource = new DOMSource(newDoc);
    Writer writer = new OutputStreamWriter(out);
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    writer.flush();

    InputStream isNewXML = new ByteArrayInputStream(out.toByteArray());
}

From source file:XSLTransform.java

public static void main(String[] args) throws TransformerException {
    // Set up streams for input, stylesheet, and output.
    // These do not have to come from or go to files. We can also use the
    // javax.xml.transform.{dom,sax} packages use DOM trees and streams of
    // SAX events as sources and sinks for documents and stylesheets.
    StreamSource input = new StreamSource(new File(args[0]));
    StreamSource stylesheet = new StreamSource(new File(args[1]));
    StreamResult output = new StreamResult(new File(args[2]));

    // Get a factory object, create a Transformer from it, and
    // transform the input document to the output document.
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(stylesheet);
    transformer.transform(input, output);
}

From source file:JAXPTransformNode.java

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);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  w  ww. ja va 2 s  .  c om

    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));
    Source source = new DOMSource(doc);

    URI uri = new File("infilename.xml").toURI();
    source.setSystemId(uri.toString());

    DefaultHandler handler = new MyHandler();
    SAXResult result = new SAXResult(handler);
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(source, result);
}

From source file:UseStylesheetPI.java

public static void main(String[] args) throws TransformerException, TransformerConfigurationException {
    String media = null, title = null, charset = null;
    try {//from  w w  w  .  j a  v a2 s.  c  o m
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Source stylesheet = tFactory.getAssociatedStylesheet(new StreamSource("fooX.xml"), media, title,
                charset);

        Transformer transformer = tFactory.newTransformer(stylesheet);

        transformer.transform(new StreamSource("fooX.xml"),
                new StreamResult(new java.io.FileOutputStream("foo.out")));

        System.out.println("************* The result is in foo.out *************");

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    XMLReader xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
        String namespace = "http://www.java2s.com/schemas.xsd";
        String pref = "ns0:";

        @Override/*from  ww w  .  j  a va  2 s  . c om*/
        public void startElement(String uri, String localName, String qName, Attributes atts)
                throws SAXException {
            super.startElement(namespace, localName, pref + qName, atts);
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            super.endElement(namespace, localName, pref + qName);
        }
    };
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    StringWriter s = new StringWriter();
    t.transform(new SAXSource(xmlReader, new InputSource("test.xml")), new StreamResult(s));
    System.out.println(s);
}

From source file:Main.java

static public void main(String[] arg) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);//from ww  w. j a v  a  2s . com
    dbf.setNamespaceAware(true);
    dbf.setIgnoringElementContentWhitespace(true);

    DocumentBuilder builder = dbf.newDocumentBuilder();

    StringReader sr = new StringReader("<tag>java2s.com</tag>");
    Document document = builder.parse(new InputSource(sr));
    deleteFirstElement(document);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer trans = tf.newTransformer();
    StringWriter sw = new StringWriter();
    trans.transform(new DOMSource(document), new StreamResult(sw));
    System.out.println(sw.toString());

}

From source file:Main.java

public static void main(String args[]) throws Exception {
    StreamSource source = new StreamSource(args[0]);
    StreamSource stylesource = new StreamSource(args[1]);

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(stylesource);

    StreamResult result = new StreamResult(System.out);
    transformer.transform(source, result);
}

From source file:Main.java

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);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml"));
    xsr.nextTag(); // Advance to statements element

    while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        File file = new File("out" + xsr.getAttributeValue(null, "account") + ".xml");
        t.transform(new StAXSource(xsr), new StreamResult(file));
    }/*from w  ww  .j a  v  a  2 s .c o  m*/
}