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

/**
 * Transform input xml given a stylesheet.
 * // w w  w . j a  v  a  2  s  .  c  o  m
 * @param styleSheet the style-sheet
 * @param xml input xml data
 * @param out output
 * @throws TransformerConfigurationException
 * @throws TransformerException
 */
public static void transform(InputStream styleSheet, InputStream xml, Writer out)
        throws TransformerConfigurationException, TransformerException {
    // Instantiate a TransformerFactory
    TransformerFactory tFactory = TransformerFactory.newInstance();

    // Use the TransformerFactory to process the  
    // stylesheet and generate a Transformer
    Transformer transformer = tFactory.newTransformer(new StreamSource(styleSheet));

    // Use the Transformer to transform an XML Source 
    // and send the output to a Result object.
    transformer.transform(new StreamSource(xml), new StreamResult(out));
}

From source file:Main.java

/**
 * returns an XML string./*from  w  w  w  . j  a v a 2 s  . c o m*/
 *
 * @param pDocument         Document XML DOM document
 * @return                  String XML string
 */
public static String getXML(Document pDocument) throws Exception {
    String retString = null;
    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.transform(new DOMSource(pDocument), new StreamResult(out));
        retString = out.toString();
        out.close();
    } catch (Exception ex) {
        throw new Exception(ex.getMessage());
    }
    return retString;
}

From source file:Main.java

private static void overwriteXmlFile(File xmlFile, Document document, Transformer transformer)
        throws FileNotFoundException, TransformerException {
    StreamResult result = new StreamResult(new PrintWriter(new FileOutputStream(xmlFile, false)));
    DOMSource source = new DOMSource(document);
    transformer.transform(source, result);
}

From source file:Main.java

/**
 * convert an XML node to an XML statement
 * @param node        current XML node/*from  w  w  w  .java  2s.c  o  m*/
 * @return            XML string
 */
public static String nodeToString(Node node) {
    //MagicBooleans.trace("nodeToString(node: " + node + ")");
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty(OutputKeys.INDENT, "no");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        System.out.println("nodeToString Transformer Exception");
    }
    String result = sw.toString();
    //MagicBooleans.trace("nodeToString() returning: " + result);
    return result;
}

From source file:Main.java

public static void toWriter(Document doc, Writer writer) {
    if (doc == null || writer == null) {
        return;/*from  w  w w  .ja  v  a2 s .com*/
    }
    try {
        Transformer tran = tf.newTransformer();
        tran.setOutputProperty(OutputKeys.INDENT, "yes");
        Source src = new DOMSource(doc);
        Result res = new StreamResult(writer);
        tran.transform(src, res);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static String toStringE(Node element) {
    try {//from w  w  w.  j  a v a 2  s .c  o m
        if (element == null) {
            return "null";
        }
        Source source = new DOMSource(element);

        StringWriter stringWriter = new StringWriter();
        try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
            Result result = new StreamResult(printWriter);

            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
            transformer.transform(source, result);
        }
        return stringWriter.toString();
    } catch (IllegalArgumentException | TransformerException ex) {
        throw new Error(ex);
    }
}

From source file:Main.java

public static String prettyPrint(Node node) {
    try {/*from  w w  w .j  a  v  a 2 s . com*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(node), new StreamResult(writer));
        return writer.toString();
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.stork.peps.test.simple.SSETestUtils.java

/**
 * Marshall.//from  ww w . j ava  2 s  .co  m
 * 
 * @param samlToken the SAML token
 * 
 * @return the byte[]
 * 
 * @throws MarshallingException the marshalling exception
 * @throws ParserConfigurationException the parser configuration exception
 * @throws TransformerException the transformer exception
 */
public static byte[] marshall(final XMLObject samlToken)
        throws MarshallingException, ParserConfigurationException, TransformerException {

    final javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    dbf.setNamespaceAware(true);
    dbf.setIgnoringComments(true);
    final javax.xml.parsers.DocumentBuilder docBuild = dbf.newDocumentBuilder();

    // Get the marshaller factory
    final MarshallerFactory marshallerFactory = Configuration.getMarshallerFactory();

    // Get the Subject marshaller
    final Marshaller marshaller = marshallerFactory.getMarshaller(samlToken);

    final Document doc = docBuild.newDocument();

    // Marshall the SAML token
    marshaller.marshall(samlToken, doc);

    // Obtain a byte array representation of the marshalled SAML object
    final DOMSource domSource = new DOMSource(doc);
    final StringWriter writer = new StringWriter();
    final StreamResult result = new StreamResult(writer);
    final TransformerFactory transFact = TransformerFactory.newInstance();
    final Transformer transformer = transFact.newTransformer();
    transformer.transform(domSource, result);

    return writer.toString().getBytes();
}

From source file:gov.nih.nci.cacis.common.test.TestUtils.java

/**
 * Will get an w3c Node element as a String
 *
 * @param node XML Node/*  www .  j av a 2  s.  c  o  m*/
 * @return String xml as String
 * @throws javax.xml.transform.TransformerException exception
 */
public static String nodeToString(Node node) throws TransformerException {
    final StringWriter sw = new StringWriter();
    final Transformer t = TransformerFactory.newInstance().newTransformer();
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(new DOMSource(node), new StreamResult(sw));
    return sw.toString();
}

From source file:Main.java

public static boolean debug(Node paramNode) {
    try {// w w w. j a v  a  2 s.c  om
        TransformerFactory localTransformerFactory = TransformerFactory.newInstance();
        Transformer localTransformer = localTransformerFactory.newTransformer();
        DOMSource localDOMSource = new DOMSource(paramNode);
        StreamResult localStreamResult = new StreamResult(System.out);
        localTransformer.transform(localDOMSource, localStreamResult);
        return true;
    } catch (Exception localException) {
        localException.printStackTrace(System.out);
    }
    return false;
}