Example usage for javax.xml.transform.stream StreamResult StreamResult

List of usage examples for javax.xml.transform.stream StreamResult StreamResult

Introduction

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

Prototype

public StreamResult(File f) 

Source Link

Document

Construct a StreamResult from a File.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL webSvcGetURL = new URL("http://www.server.net/Webservices");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(webSvcGetURL.openStream()));
    SAXSource saxSource = new SAXSource(new InputSource(bufferedReader));
    String curDir = new File(".").getCanonicalPath();
    StreamSource xlstStreamSource = new StreamSource(new File(curDir + File.separator + "style.xsl"));
    File resultHTMLFile = new File(curDir + File.separator + "output.html");

    StreamResult streamResult = new StreamResult(resultHTMLFile);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(xlstStreamSource);
    transformer.transform((Source) saxSource, streamResult);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    serializer.setOutputProperty("{http://xml.customer.org/xslt}indent-amount", "2");
    Source xmlSource = new SAXSource(new InputSource(
            new ByteArrayInputStream("<a><b><c/><d>text D</d><e value='0'/></b></a>".getBytes())));
    StreamResult res = new StreamResult(new ByteArrayOutputStream());
    serializer.transform(xmlSource, res);
    System.out.println(new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()));
}

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[] args) throws Exception {
    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new InputSource("data.xml"));

    XPath xpath = XPathFactory.newInstance().newXPath();
    NodeList nodes = (NodeList) xpath.evaluate("//employee/name[text()='old']", doc, XPathConstants.NODESET);

    for (int idx = 0; idx < nodes.getLength(); idx++) {
        nodes.item(idx).setTextContent("new value");
    }//from w w w. j  av a  2 s. c o m
    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.transform(new DOMSource(doc), new StreamResult(new File("data_new.xml")));
}

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

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 factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder parser = factory.newDocumentBuilder();

    Document doc;//  w  w w.  j  a  va  2 s .  c om
    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 {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document document = dbf.newDocumentBuilder().parse(new File("input.xml"));

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    XPathExpression expression = xpath.compile("//A/B[C/E/text()=13]");

    Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
    b13Node.getParentNode().removeChild(b13Node);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(document), new StreamResult(System.out));
}

From source file:Main.java

static public void main(String[] arg) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(true);/*from  w  w  w .  java  2s. co m*/
    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 {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document document = dbf.newDocumentBuilder().parse(new File("input.xml"));

    XPathFactory xpf = XPathFactory.newInstance();
    XPath xpath = xpf.newXPath();
    XPathExpression expression = xpath.compile("//data/user/username[text()='simple']");
    Node b13Node = (Node) expression.evaluate(document, XPathConstants.NODE);
    b13Node = b13Node.getParentNode();
    b13Node.getParentNode().removeChild(b13Node);

    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    t.transform(new DOMSource(document), new StreamResult(System.out));
}