Example usage for javax.xml.transform TransformerFactory newInstance

List of usage examples for javax.xml.transform TransformerFactory newInstance

Introduction

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

Prototype

public static TransformerFactory newInstance() throws TransformerFactoryConfigurationError 

Source Link

Document

Obtain a new instance of a TransformerFactory .

Usage

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

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

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

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    while (xsr.nextTag() == XMLStreamConstants.START_ELEMENT) {
        DOMResult result = new DOMResult();
        t.transform(new StAXSource(xsr), result);
        Node domNode = result.getNode();
    }//from w  w  w  .  j  a  va 2  s  . com
}

From source file:PrintDOM.java

public static void main(String[] args) throws Exception {
    DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = parser.parse(new InputSource("zooinventory.xml"));
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    Source source = new DOMSource(document);
    Result output = new StreamResult(System.out);
    transformer.transform(source, 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[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = factory.newDocumentBuilder();

    Document doc;/*from ww  w  . j  a  v  a  2 s. c  o m*/
    doc = parser.parse(args[0]);

    TransformerFactory transFactory = TransformerFactory.newInstance();
    System.out.println(transFactory.getClass().getName());
    transFactory.setAttribute("indent-number", 2);
    Transformer idTransform = transFactory.newTransformer();
    idTransform.setOutputProperty(OutputKeys.METHOD, "xml");
    idTransform.setOutputProperty(OutputKeys.INDENT, "yes");
    Source input = new DOMSource(doc);
    Result output = new StreamResult(System.out);
    idTransform.transform(input, output);
}

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));
    }// w w w .j a  v  a 2s. c o m
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);/*from   w  w  w.  j av  a2s. com*/

    factory.setExpandEntityReferences(false);

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

    Transformer xformer = TransformerFactory.newInstance().newTransformer();

    xformer.setOutputProperty(OutputKeys.METHOD, "text");

    Source source = new DOMSource(doc);
    Result result = new StreamResult(new File("outfilename.xml"));
    xformer.transform(source, result);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from  w w  w. ja  v a  2 s  .co m

    factory.setExpandEntityReferences(false);

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

    Transformer xformer = TransformerFactory.newInstance().newTransformer();

    xformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "publicId");
    xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "systemId");

    Source source = new DOMSource(doc);
    Result result = new StreamResult(new File("outfilename.xml"));
    xformer.transform(source, result);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse(new File("in.xml"));

    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty("indent", "yes");
    StringWriter sw = new StringWriter();
    StreamResult result = new StreamResult(sw);

    NodeList nl = doc.getDocumentElement().getChildNodes();
    DOMSource source = null;/*from ww w  .j  av a  2s  .  co m*/
    for (int x = 0; x < nl.getLength(); x++) {
        Node e = nl.item(x);
        if (e instanceof Element) {
            source = new DOMSource(e);
            break;
        }
    }
    transformer.transform(source, result);
    System.out.println(sw.toString());
}

From source file:Main.java

static public void main(String[] arg) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.parse("foo.xml");

    TransformerFactory tranFactory = TransformerFactory.newInstance();
    Transformer aTransformer = tranFactory.newTransformer();

    NodeList list = doc.getFirstChild().getChildNodes();

    for (int i = 0; i < list.getLength(); i++) {
        Node element = list.item(i).cloneNode(true);

        if (element.hasChildNodes()) {
            Source src = new DOMSource(element);
            FileOutputStream fs = new FileOutputStream("k" + i + ".xml");
            Result dest = new StreamResult(fs);
            aTransformer.transform(src, dest);
            fs.close();//from  w w  w  . j a v  a 2 s  .  co m
        }
    }
}