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

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

Introduction

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

Prototype

public StreamSource(File f) 

Source Link

Document

Construct a StreamSource from a File.

Usage

From source file:Main.java

public static void transform(Document src, Writer dst, String xsl) throws TransformerException {

    DOMSource dsrc = new DOMSource(src);
    StreamResult res = new StreamResult(dst);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer(new StreamSource(xsl));
    t.setOutputProperty(OutputKeys.METHOD, "xml");
    t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    t.transform(dsrc, res);/*w w w .j  a v a 2 s  . c  o m*/

}

From source file:Main.java

public static String prettyPrintXML(String xml) throws TransformerException {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StreamResult result = new StreamResult(new StringWriter());
    StreamSource source = new StreamSource(new StringReader(xml));
    transformer.transform(source, result);
    return result.getWriter().toString();
}

From source file:Main.java

/**
 * This method validate XML by input XML as String and XSD path to File.
 *
 * @param xml     input XML as String// w  w w.j a va 2  s  .c  o  m
 * @param xsdPath input XSD File Path
 *
 * @return true or false, valid or not
 */
public static boolean validateXMLByXSD(String xml, String xsdPath) {
    try {
        SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new URL(xsdPath)).newValidator()
                .validate(new StreamSource(new StringReader(xml)));
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:Main.java

/**
 * @param xml/* w  w  w.j a  v  a 2s .co m*/
 */
public static void prettyPrint(String xml) {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    try {
        transformer.transform(xmlInput, xmlOutput);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String formattedxml = xmlOutput.getWriter().toString();
    System.out.println(formattedxml);

}

From source file:Main.java

public static String validate(String xsdPath, String xmlPath) {
    String response = "OK";

    try {//from   w w  w. j a va  2  s  .  com
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
        File schemaLocation = new File(xsdPath);
        Schema schema = factory.newSchema(schemaLocation);
        Validator validator = schema.newValidator();
        Source source = new StreamSource(xmlPath);
        Result result = null;
        validator.validate(source, result);
    } catch (Exception e) {
        response = e.getMessage();
    }

    return response;
}

From source file:Main.java

public static Object parseXmlStringToBeanByJAXB(String xml, Class clase) throws Exception {

    JAXBContext context = JAXBContext.newInstance(clase);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream is = null;/*from   ww w  .  j  ava 2s  .c  om*/
    try {
        is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    if (is == null) {
        return null;
    }

    JAXBElement elm = unmarshaller.unmarshal(new StreamSource(is), clase);

    return elm.getValue();

}

From source file:Main.java

private static String isXmlPassingSchemaValidation(InputStream xml, InputStream xsd) throws IOException {
    Source xmlSource = new StreamSource(xml);

    try {//from   ww  w.  j  ava2  s  . c o m
        SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Schema schema = factory.newSchema(new StreamSource(xsd));
        Validator validator = schema.newValidator();
        validator.validate(xmlSource);
        return null;
    } catch (SAXException e) {
        return e.toString();
    }

}

From source file:Main.java

/**
 * @param xml//from   www  .j  a v  a  2  s.  c  o m
 */
public static String prettyPrintToString(String xml) {
    Source xmlInput = new StreamSource(new StringReader(xml));
    StreamResult xmlOutput = new StreamResult(new StringWriter());
    Transformer transformer = null;
    try {
        transformer = TransformerFactory.newInstance().newTransformer();
    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerFactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    try {
        transformer.transform(xmlInput, xmlOutput);
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    String formattedxml = xmlOutput.getWriter().toString();
    return formattedxml;

}

From source file:Main.java

public static void transformDocument(Document document, Writer out, File stylesheet)
        throws TransformerException {
    document.normalizeDocument();/*from   ww w.  ja v  a  2  s  .  com*/
    Transformer idTransform = null;
    TransformerFactory transFactory = TransformerFactory.newInstance();
    StreamSource stylesource = new StreamSource(stylesheet);
    idTransform = transFactory.newTransformer(stylesource);
    Source source = new DOMSource(document);
    Result result = new StreamResult(out);
    idTransform.transform(source, result);
}

From source file:Main.java

public static void xsl(String inFilename, String outFilename, String xslFilename) {
    try {//from  w w w  . j  ava  2 s  .  c o  m
        TransformerFactory factory = TransformerFactory.newInstance();
        Templates template = factory.newTemplates(new StreamSource(new FileInputStream(xslFilename)));
        Transformer xformer = template.newTransformer();
        Source source = new StreamSource(new FileInputStream(inFilename));
        Result result = new StreamResult(new FileOutputStream(outFilename));
        xformer.transform(source, result);
    } catch (FileNotFoundException e) {
    } catch (TransformerConfigurationException e) {
    } catch (TransformerException e) {
        SourceLocator locator = e.getLocator();
        int col = locator.getColumnNumber();
        int line = locator.getLineNumber();
        String publicId = locator.getPublicId();
        String systemId = locator.getSystemId();
    }
}