Example usage for javax.xml.transform.dom DOMResult DOMResult

List of usage examples for javax.xml.transform.dom DOMResult DOMResult

Introduction

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

Prototype

public DOMResult(Node node) 

Source Link

Document

Use a DOM node to create a new output target.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    Templates template = factory.newTemplates(new StreamSource(new FileInputStream("xsl.xlt")));
    Transformer xformer = template.newTransformer();
    Source source = new StreamSource(new FileInputStream("in.xml"));
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = builder.newDocument();
    Result result = new DOMResult(doc);
    xformer.transform(source, result);/* w ww .ja va 2s. c o  m*/
}

From source file:Main.java

public static NodeList selectNodeList(final Node context, Transformer t) throws Exception {
    // Create result document with top element "result"
    DocumentFragment df = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
            .createDocumentFragment();//from   w ww .  j  av a 2  s . c o  m

    Result result = new DOMResult(df);

    // Transform result into DocumentFragment
    t.transform(new DOMSource(context), result);

    // Return list of nodes in DocumentFragment
    return df.getChildNodes();
}

From source file:Main.java

static public Document cloneDocument(Document document) {
    if (document == null)
        return null;
    Document result = newDocument();
    try {/*from   w  w  w.  j  a  v a2  s  .  c o  m*/
        identityTransformer.transform(new DOMSource(document), new DOMResult(result));
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static Document xslt(InputStream stylesheet, Document input)
        throws FileNotFoundException, TransformerException, ParserConfigurationException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheet));
    Document result = newDocument();
    DOMResult domResult = new DOMResult(result);
    transformer.transform(new DOMSource(input), domResult);
    return result;
}

From source file:Main.java

public static Node trance(Node context, Transformer t) throws Exception {
    DocumentFragment df = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
            .createDocumentFragment();// w ww .  ja v a 2 s .  co  m

    Result result = new DOMResult(df);

    t.transform(new DOMSource(context), result);

    return df;
}

From source file:Main.java

public static Node trance(Node context, Transformer t) throws Exception {

    // Create result document with top element "result"
    DocumentFragment df = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
            .createDocumentFragment();//w  ww . j  a  v  a 2 s  .c o m

    Result result = new DOMResult(df);

    // Transform result into DocumentFragment
    t.transform(new DOMSource(context), result);

    // Return list of nodes in DocumentFragment
    return df;
}

From source file:Main.java

public static NodeList selectNodeList(Node context, Transformer t) throws Exception {
    DocumentFragment df = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
            .createDocumentFragment();//  w  ww .j a  v a  2  s .  com

    Result result = new DOMResult(df);

    t.transform(new DOMSource(context), result);

    return df.getChildNodes();
}

From source file:jlib.xml.XMLUtil.java

public static Document LoadXML(Source file) {
    try {//from w  w w .j  av a  2 s  . co  m
        DocumentBuilderFactory dbf = DocumentBuilderFactoryImpl.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();
        Result res = new DOMResult(doc);
        TransformerFactory tr = TransformerFactory.newInstance();
        Transformer xformer = tr.newTransformer();
        xformer.transform(file, res);

        return doc;
    } catch (Exception e) {
        String csError = e.toString();
        Log.logImportant(csError);
        Log.logImportant("ERROR while loading XML " + file.toString());
    }
    return null;
}

From source file:Main.java

/**
 * Applies a stylesheet (that receives parameters) to a given xml document.
 * //from   w  w  w  .  j  a  v  a 2  s .com
 * @param xmlDocument
 *            the xml document to be transformed
 * @param parameters
 *            the hashtable with the parameters to be passed to the
 *            stylesheet
 * @param xsltFilename
 *            the filename of the stylesheet
 * @return the transformed xml document
 * @throws Exception
 */
public static Document transformDocument(Document xmlDocument, Map<String, String> parameters,
        String xsltFilename) throws Exception {

    // Generate a Transformer.
    Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename));

    // set transformation parameters
    if (parameters != null) {
        for (Map.Entry<String, String> param : parameters.entrySet()) {
            transformer.setParameter(param.getKey(), param.getValue());
        }

    }

    // Create an empty DOMResult object for the output.
    DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
    dFactory.setNamespaceAware(true);
    DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
    Document dstDocument = dBuilder.newDocument();

    DOMResult domResult = new DOMResult(dstDocument);

    // Perform the transformation.
    transformer.transform(new DOMSource(xmlDocument), domResult);
    // Now you can get the output Node from the DOMResult.
    return dstDocument;
}

From source file:Main.java

/**
 * Applies a stylesheet (that receives parameters) to a given xml document.
 *
 * @param xmlDocument  the xml document to be transformed
 * @param parameters   the hashtable with the parameters to be passed to the
 *                     stylesheet// w w w.j a va 2s  .c  o  m
 * @param xsltFilename the filename of the stylesheet
 * @return the transformed xml document
 * @throws Exception
 */
public static Document transformDocument(Document xmlDocument, Map<String, String> parameters,
        String xsltFilename) throws TransformerException, ParserConfigurationException {

    File xslFile = new File(xsltFilename);
    if (xslFile.exists()) {

        // Generate a Transformer.
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer(new StreamSource(xsltFilename));

        if (transformer != null) {

            // set transformation parameters
            if (parameters != null) {
                for (Map.Entry<String, String> param : parameters.entrySet()) {
                    transformer.setParameter(param.getKey(), param.getValue());
                }
            }

            // Create an empty DOMResult object for the output.
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            dFactory.setNamespaceAware(true);
            DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
            Document dstDocument = dBuilder.newDocument();

            DOMResult domResult = new DOMResult(dstDocument);

            // Perform the transformation.
            transformer.transform(new DOMSource(xmlDocument), domResult);
            // Now you can get the output Node from the DOMResult.
            return dstDocument;
        }
    }
    return null;
}