Example usage for javax.xml.transform.sax SAXSource SAXSource

List of usage examples for javax.xml.transform.sax SAXSource SAXSource

Introduction

In this page you can find the example usage for javax.xml.transform.sax SAXSource SAXSource.

Prototype

public SAXSource(InputSource inputSource) 

Source Link

Document

Create a SAXSource, using a SAX InputSource.

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:MainClass.java

public static void main(String[] args) throws IOException {
    File documentFile = new File(args[0]);
    File schemaFile = new File(args[1]);

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

    Schema schema = null;/*from www .jav  a  2 s. co m*/
    try {
        schema = factory.newSchema(schemaFile);
    } catch (SAXException e) {
        fail(e);
    }

    Validator validator = schema.newValidator();

    SAXSource source = new SAXSource(new InputSource(new FileReader(documentFile)));

    try {
        validator.validate(source);
    } catch (SAXException e) {
        fail(e);
    }
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser parser = null;/*from  w w  w.  ja  v a  2 s.  com*/
    spf.setNamespaceAware(true);
    try {
        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        spf.setSchema(sf.newSchema(new SAXSource(new InputSource(new StringReader(schemaString)))));

        parser = spf.newSAXParser();
    } catch (SAXException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    } catch (ParserConfigurationException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    MySAXHandler handler = new MySAXHandler();
    System.out.println(schemaString);
    parser.parse(new InputSource(new StringReader(xmlString)), handler);
}

From source file:Main.java

public static boolean doesXMLMatchXSD(String xmlUrl) throws SAXException, FileNotFoundException, IOException {

    boolean doesMatchXSD = false;

    SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    SAXSource sourceXSD = new SAXSource(new InputSource(new FileInputStream(new File(xsdUrl))));
    Schema schema = factory.newSchema(sourceXSD);
    Validator validator = (Validator) schema.newValidator();
    validator.validate(new StreamSource(new File(xmlUrl)));

    doesMatchXSD = true;//from  w  w w  . jav a 2s.c  om
    return doesMatchXSD;
}

From source file:Main.java

public static String formatXML(String xml) {
    try {//from ww w .j  a  va2 s.  c  o m
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());
        serializer.transform(xmlSource, res);
        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());
    } catch (Exception e) {

        return xml;
    }
}

From source file:Main.java

public static String prettyPrintXML(String xml, int indentAmount)
        throws TransformerConfigurationException, TransformerException {

    Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();

    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentAmount));
    Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
    StreamResult res = new StreamResult(new ByteArrayOutputStream());
    serializer.transform(xmlSource, res);

    return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());
}

From source file:Main.java

public static String prettyFormat(String xml) {
    if (xml == null || xml.isEmpty() || !xml.contains("<")) {
        //          System.out.println("Why?"+xml.startsWith("<", 0));
        return xml;
    }/*from  www.  ja  v a  2  s  .  co  m*/
    try {
        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");
        Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());
        serializer.transform(xmlSource, res);
        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()).replace("><", ">\n<");
    } catch (Exception e) {
        System.out.println("prettyFormat: Error.." + e.getMessage());
        //TODO log error
        return xml.replace("<", "\n<");
        //            return xml.replace("><", ">\n<");
    }
}

From source file:Main.java

public static void formatXML(Reader xml, Reader xsl, URIResolver resolver, Writer output) throws Exception {
    try {//  www  . j  a  va2s. c  o  m
        try {
            try {
                Source xmlSource = new SAXSource(new InputSource(xml));
                Source xslSource = new SAXSource(new InputSource(xsl));
                Result result = new StreamResult(output);
                formatXML(xmlSource, xslSource, resolver, result);
            } finally {
                output.close();
            }
        } finally {
            xsl.close();
        }
    } finally {
        xml.close();
    }
}

From source file:com.github.jjYBdx4IL.utils.fma.FMAConfig.java

public static String formatXml(String xml) {

    try {//from  www. ja va  2 s  .c  o m
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();

        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());

        serializer.transform(xmlSource, res);

        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());

    } catch (IllegalArgumentException | TransformerException e) {
        LOG.error("", e);
        return xml;
    }
}