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 Document formatXML(Document xml, Transformer transformer) throws Exception {
    DOMSource xmlSource = new DOMSource(xml);
    Document resultDoc = newDocument();
    DOMResult result = new DOMResult(resultDoc);
    transformer.transform(xmlSource, result);
    return resultDoc;
}

From source file:uk.ac.bbsrc.tgac.miso.core.util.TaxonomyUtils.java

public static String checkScientificNameAtNCBI(String scientificName) {
    try {/*  w  ww  .j  a v  a2  s.c o m*/
        String query = ncbiEntrezUtilsURL + "db=taxonomy&term=" + URLEncoder.encode(scientificName, "UTF-8");
        final HttpClient httpclient = HttpClientBuilder.create().build();
        HttpGet httpget = new HttpGet(query);
        try {
            HttpResponse response = httpclient.execute(httpget);
            String out = parseEntity(response.getEntity());
            log.info(out);
            try {
                DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document d = docBuilder.newDocument();

                TransformerFactory.newInstance().newTransformer()
                        .transform(new StreamSource(new UnicodeReader(out)), new DOMResult(d));

                NodeList nl = d.getElementsByTagName("Id");
                for (int i = 0; i < nl.getLength(); i++) {
                    Element e = (Element) nl.item(i);
                    return e.getTextContent();
                }
            } catch (ParserConfigurationException e) {
                log.error("check scientific name at NCBI", e);
            } catch (TransformerException e) {
                log.error("check scientific name at NCBI", e);
            }
        } catch (ClientProtocolException e) {
            log.error("check scientific name at NCBI", e);
        } catch (IOException e) {
            log.error("check scientific name at NCBI", e);
        }
    } catch (UnsupportedEncodingException e) {
        log.error("check scientific name at NCBI", e);
    }
    return null;
}

From source file:jlib.xml.XMLUtil.java

public static Document loadXML(ByteArrayInputStream byteArrayInputStream) {
    try {//from ww  w  .  jav  a 2  s .co  m
        StreamSource streamSource = new StreamSource(byteArrayInputStream);
        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(streamSource, res);

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

From source file:Main.java

/**
 *
 * @param source/*from ww  w  . j  av a  2 s.  co m*/
 * @param xsltSource
 * @return
 * @throws TransformerConfigurationException
 * @throws JAXBException
 * @throws TransformerException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static synchronized Document transform(Element source, InputStream xsltSource)
        throws TransformerConfigurationException, JAXBException, TransformerException,
        ParserConfigurationException, SAXException, IOException {
    Document obj = null;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    if (xsltSource != null) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer;
        transformer = factory.newTransformer(new StreamSource(xsltSource));
        obj = dbf.newDocumentBuilder().newDocument();
        Result result = new DOMResult(obj);
        transformer.transform(new DOMSource(source), result);
    }
    return obj;
}

From source file:Main.java

/**
 * Converts an XML String to a Document.
 *
 * @param string//from w  w w.jav a  2  s .c o m
 * @return
 * @throws TransformerException
 * @throws ParserConfigurationException
 */
public static Document toDocument(String string) throws TransformerException, ParserConfigurationException {

    // if there is a byte order mark, strip it off.
    // otherwise, we get a org.xml.sax.SAXParseException: Content is not allowed in prolog
    if (string.startsWith("\uFEFF")) {
        string = string.substring(1);
    }

    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();

    // StringReader source
    Source source = new StreamSource(new StringReader(string));

    // Document result
    DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document document = builder.newDocument();
    Result result = new DOMResult(document);

    transformer.transform(source, result);
    return document;
}

From source file:Main.java

/**
 *
 * @param source//from  w w  w .jav a  2 s .  co m
 * @param xsltSource
 * @return
 * @throws TransformerConfigurationException
 * @throws JAXBException
 * @throws TransformerException
 * @throws ParserConfigurationException
 * @throws SAXException
 * @throws IOException
 */
public static synchronized Document deserializeToDom(InputStream source, InputStream xsltSource)
        throws TransformerConfigurationException, JAXBException, TransformerException,
        ParserConfigurationException, SAXException, IOException {
    Document obj;
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    if (xsltSource != null) {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer(new StreamSource(xsltSource));
        obj = dbf.newDocumentBuilder().newDocument();
        Result result = new DOMResult(obj);
        transformer.transform(new StreamSource(source), result);
    } else {
        obj = dbf.newDocumentBuilder().parse(source);

    }
    return obj;
}

From source file:Main.java

public static Document formatXML(Document xml, URIResolver resolver, String... xsls) throws Exception {
    TransformerFactory factory = TransformerFactory.newInstance();
    for (String xsl : xsls) {
        DOMSource xmlSource = new DOMSource(xml);
        Source xslSource = resolver.resolve(xsl, "");
        Templates t = factory.newTemplates(xslSource);
        Transformer transformer = t.newTransformer();
        transformer.setURIResolver(resolver);
        Document resultDoc = newDocument();
        DOMResult result = new DOMResult(resultDoc);
        transformer.transform(xmlSource, result);
        xml = resultDoc;/* ww  w  .  j a  va2  s. c o m*/
    }
    return xml;
}

From source file:Main.java

public static Document transform(Document dom, Document xslt) {
    try {/*from  w ww .  j  a v a 2  s .c o m*/
        DOMSource xsltSource = new DOMSource(xslt);
        Transformer transformer = transFactory.newTransformer(xsltSource);
        DOMSource source = new DOMSource(dom);
        Document out = newDocument();
        DOMResult result = new DOMResult(out);
        transformer.transform(source, result);
        return out;
    } catch (Exception e) {
        throw new RuntimeException("Could not write dom tree '" + dom.getBaseURI() + "'!", e);
    }
}

From source file:ca.nines.ise.util.XMLDriver.java

/**
 * Parse the file and add location information to the document and each node
 * in the document./*from   ww w .  j  av  a2  s .  co  m*/
 *
 * @param in
 * @return Document
 * @throws TransformerException
 * @throws IOException
 */
public Document drive(File in) throws TransformerException, IOException {
    Document doc = docBuilder.newDocument();
    DOMResult domResult = new DOMResult(doc);
    LocationAnnotator locationAnnotator = new LocationAnnotator(xmlReader, doc);

    String systemId = in.getCanonicalPath();
    InputSource inputSource = new InputSource(systemId);
    SAXSource saxSource = new SAXSource(locationAnnotator, inputSource);
    nullTransformer.transform(saxSource, domResult);

    return doc;
}

From source file:org.n52.smartsensoreditor.service.TestValidation.java

private String validateResource(String resourcePath) throws SAXException, IOException {
    Document lReport = DOMUtil.newDocument(true);
    Document input = documentBuilder.parse(TestValidation.class.getResourceAsStream(resourcePath));

    Source lSource = new DOMSource(input);
    Result lResult = new DOMResult(lReport);
    xsltTransformer.setRulesetSystemID(validator.getRulesetSystemID());
    xsltTransformer.transform(lSource, lResult);

    String resultString = DOMUtil.convertToString(lReport, true);
    return resultString;
}