Example usage for org.xml.sax.helpers XMLReaderFactory createXMLReader

List of usage examples for org.xml.sax.helpers XMLReaderFactory createXMLReader

Introduction

In this page you can find the example usage for org.xml.sax.helpers XMLReaderFactory createXMLReader.

Prototype

public static XMLReader createXMLReader() throws SAXException 

Source Link

Document

Obtains a new instance of a org.xml.sax.XMLReader .

Usage

From source file:Main.java

public static void main(String args[]) throws Exception {
    XMLReader xmlReader = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
        String namespace = "http://www.java2s.com/schemas.xsd";
        String pref = "ns0:";

        @Override/*w  ww. j  a va  2  s.c o  m*/
        public void startElement(String uri, String localName, String qName, Attributes atts)
                throws SAXException {
            super.startElement(namespace, localName, pref + qName, atts);
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
            super.endElement(namespace, localName, pref + qName);
        }
    };
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    StringWriter s = new StringWriter();
    t.transform(new SAXSource(xmlReader, new InputSource("test.xml")), new StreamResult(s));
    System.out.println(s);
}

From source file:SAX2SAX.java

public static void main(String[] args)
        throws TransformerException, TransformerConfigurationException, SAXException, IOException {

    // Instantiate a TransformerFactory.
    TransformerFactory tFactory = TransformerFactory.newInstance();
    // Determine whether the TransformerFactory supports The use of SAXSource 
    // and SAXResult
    if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) {
        // Cast the TransformerFactory.
        SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
        // Create a ContentHandler to handle parsing of the stylesheet.
        TemplatesHandler templatesHandler = saxTFactory.newTemplatesHandler();

        // Create an XMLReader and set its ContentHandler.
        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setContentHandler(templatesHandler);

        // Parse the stylesheet.                       
        reader.parse("birds.xsl");

        //Get the Templates object from the ContentHandler.
        Templates templates = templatesHandler.getTemplates();
        // Create a ContentHandler to handle parsing of the XML source.  
        TransformerHandler handler = saxTFactory.newTransformerHandler(templates);
        // Reset the XMLReader's ContentHandler.
        reader.setContentHandler(handler);

        // Set the ContentHandler to also function as a LexicalHandler, which
        // includes "lexical" events (e.g., comments and CDATA). 
        reader.setProperty("http://xml.org/sax/properties/lexical-handler", handler);

        FileOutputStream fos = new FileOutputStream("birds.out");

        java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
        xmlProps.setProperty("indent", "yes");
        xmlProps.setProperty("standalone", "no");
        Serializer serializer = SerializerFactory.getSerializer(xmlProps);
        serializer.setOutputStream(fos);

        // Set the result handling to be a serialization to the file output stream.
        Result result = new SAXResult(serializer.asContentHandler());
        handler.setResult(result);/* w w  w.  j ava  2s  . c om*/

        // Parse the XML input document.
        reader.parse("birds.xml");

        System.out.println("************* The result is in birds.out *************");
    } else
        System.out.println("The TransformerFactory does not support SAX input and SAX output");
}

From source file:Pipe.java

public static void main(String[] args)
        throws TransformerException, TransformerConfigurationException, SAXException, IOException {
    // Instantiate  a TransformerFactory.
    TransformerFactory tFactory = TransformerFactory.newInstance();
    // Determine whether the TransformerFactory supports The use uf SAXSource 
    // and SAXResult
    if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) {
        // Cast the TransformerFactory to SAXTransformerFactory.
        SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
        // Create a TransformerHandler for each stylesheet.
        TransformerHandler tHandler1 = saxTFactory.newTransformerHandler(new StreamSource("foo1.xsl"));
        TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource("foo2.xsl"));
        TransformerHandler tHandler3 = saxTFactory.newTransformerHandler(new StreamSource("foo3.xsl"));

        // Create an XMLReader.
        XMLReader reader = XMLReaderFactory.createXMLReader();
        reader.setContentHandler(tHandler1);
        reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler1);

        tHandler1.setResult(new SAXResult(tHandler2));
        tHandler2.setResult(new SAXResult(tHandler3));

        // transformer3 outputs SAX events to the serializer.
        java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
        xmlProps.setProperty("indent", "yes");
        xmlProps.setProperty("standalone", "no");
        Serializer serializer = SerializerFactory.getSerializer(xmlProps);
        serializer.setOutputStream(System.out);
        tHandler3.setResult(new SAXResult(serializer.asContentHandler()));

        // Parse the XML input document. The input ContentHandler and output ContentHandler
        // work in separate threads to optimize performance.   
        reader.parse("foo.xml");
    }/*from  w  w w. j a  v  a  2  s.c om*/
}

From source file:UseXMLFilters.java

public static void main(String[] args)
        throws TransformerException, TransformerConfigurationException, SAXException, IOException {
    // Instantiate  a TransformerFactory.
    TransformerFactory tFactory = TransformerFactory.newInstance();
    // Determine whether the TransformerFactory supports The use uf SAXSource 
    // and SAXResult
    if (tFactory.getFeature(SAXSource.FEATURE) && tFactory.getFeature(SAXResult.FEATURE)) {
        // Cast the TransformerFactory to SAXTransformerFactory.
        SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
        // Create an XMLFilter for each stylesheet.
        XMLFilter xmlFilter1 = saxTFactory.newXMLFilter(new StreamSource("foo1.xsl"));
        XMLFilter xmlFilter2 = saxTFactory.newXMLFilter(new StreamSource("foo2.xsl"));
        XMLFilter xmlFilter3 = saxTFactory.newXMLFilter(new StreamSource("foo3.xsl"));

        // Create an XMLReader.
        XMLReader reader = XMLReaderFactory.createXMLReader();

        // xmlFilter1 uses the XMLReader as its reader.
        xmlFilter1.setParent(reader);/*from   w w w .ja  v  a  2s  . c  om*/

        // xmlFilter2 uses xmlFilter1 as its reader.
        xmlFilter2.setParent(xmlFilter1);

        // xmlFilter3 uses xmlFilter2 as its reader.
        xmlFilter3.setParent(xmlFilter2);

        // xmlFilter3 outputs SAX events to the serializer.
        java.util.Properties xmlProps = OutputPropertiesFactory.getDefaultMethodProperties("xml");
        xmlProps.setProperty("indent", "yes");
        xmlProps.setProperty("standalone", "no");
        Serializer serializer = SerializerFactory.getSerializer(xmlProps);
        serializer.setOutputStream(System.out);
        xmlFilter3.setContentHandler(serializer.asContentHandler());

        // Perform the series of transformations as follows:
        //   - transformer3 gets its parent (transformer2) as the XMLReader/XMLFilter
        //     and calls transformer2.parse(new InputSource("foo.xml")).
        //   - transformer2 gets its parent (transformer1) as the XMLReader/XMLFilter
        //     and calls transformer1.parse(new InputSource("foo.xml")). 
        //   - transformer1 gets its parent (reader, a SAXParser) as the XMLReader 
        //     and calls reader.parse(new InputSource("foo.xml")).
        //   - reader parses the XML document and sends the SAX parse events to transformer1, 
        //     which performs transformation 1 and sends the output to transformer2.
        //   - transformer2 parses the transformation 1 output, performs transformation 2, and 
        //     sends the output to transformer3.
        //   - transformer3 parses the transformation 2 output, performs transformation 3,
        //     and sends the output to the serializer.
        xmlFilter3.parse(new InputSource("foo.xml"));
    }
}

From source file:Main.java

public static XMLReader parse(InputStream in, ContentHandler handler) throws SAXException, IOException {

    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setContentHandler(handler);//from   w  ww  . ja v a  2  s  .  c  o m
    reader.parse(new InputSource(in));
    in.close();
    return reader;
}

From source file:Main.java

/**
 * Get an instance of an XML reader from the XMLReaderFactory.
 *
 * @return the XMLReader.//from w  w w  .j  ava 2s. c  o m
 */
public static XMLReader getXmlReader() {
    try {
        return XMLReaderFactory.createXMLReader();
    } catch (final SAXException e) {
        throw new RuntimeException("Unable to create XMLReader", e);
    }
}

From source file:com.surveypanel.form.xml.ParserHelper.java

/**
 * Return a reader to parse content/*from   ww w .ja  v  a2  s  . c o  m*/
 * @param path
 * @return
 */
protected static FormValuesHandler getReader(InputStream is) {
    FormValuesHandler formValuesHandler = new FormValuesHandler();
    XMLReader parser = null;
    try {
        InputStreamReader inputStreamReader = new InputStreamReader(is, "UTF-8");
        parser = XMLReaderFactory.createXMLReader();
        parser.setContentHandler(formValuesHandler);
        parser.setErrorHandler(formValuesHandler);
        InputSource inputSource = new InputSource(inputStreamReader);
        parser.parse(inputSource);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return formValuesHandler;
}

From source file:Main.java

/**
 * //from   w  w w  .  j  a v a  2  s .  c om
 */
public static XMLReader getXmlReader() throws SAXException {
    XMLReader parser = XMLReaderFactory.createXMLReader();
    return parser;
}

From source file:Main.java

/**
 * Filters an XML document.//from  ww  w .j  a v a2 s  .co m
 * 
 * @param source the input source for the XML document
 * @param filter the filter
 * 
 * @return an input source for the resulting document
 * 
 * @throws Exception
 */
public static InputSource filterXml(InputSource source, XMLFilter filter) throws Exception {
    // Create filter, which uses a "real" XMLReader but also removes the
    // attributes
    XMLReader reader = XMLReaderFactory.createXMLReader();
    filter.setParent(reader);

    // Create a Transformer
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();

    // Transform the source tree, applying the filter, and store the result
    // tree in a character array
    CharArrayWriter writer = new CharArrayWriter();
    t.transform(new SAXSource(filter, source), new StreamResult(writer));

    // Return a new InputSource using the result tree
    return new InputSource(new CharArrayReader(writer.toCharArray()));
}

From source file:net.sf.janos.model.xml.ResultParser.java

/**
 * @param xml//w  w w .j  ava 2s .c  o m
 * @return a list of Entrys from the given xml string.
 * @throws IOException
 * @throws SAXException
 */
public static List<Entry> getEntriesFromStringResult(String xml) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    EntryHandler handler = new EntryHandler();
    reader.setContentHandler(handler);
    try {
        reader.parse(new InputSource(new StringReader(xml)));
    } catch (IOException e) {
        // This should never happen - we're not performing I/O!
        LOG.error("Could not parse entries: ", e);
    }
    return handler.getArtists();
}