Example usage for org.xml.sax XMLReader parse

List of usage examples for org.xml.sax XMLReader parse

Introduction

In this page you can find the example usage for org.xml.sax XMLReader parse.

Prototype

public void parse(String systemId) throws IOException, SAXException;

Source Link

Document

Parse an XML document from a system identifier (URI).

Usage

From source file:FragmentContentHandler.java

public static void main(String[] args) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser sp = spf.newSAXParser();
    XMLReader xr = sp.getXMLReader();

    xr.setContentHandler(new FragmentContentHandler(xr));
    xr.parse(new InputSource(new FileInputStream("input.xml")));
}

From source file:MyHandler.java

public static void main(String[] args) throws Exception {
    XMLReader parser = org.xml.sax.helpers.XMLReaderFactory.createXMLReader();
    ContentHandler contentHandler = new MyHandler();
    parser.setContentHandler(contentHandler);

    parser.parse("foo.xml");
}

From source file:MyErrorHandler.java

static public void main(String[] arg) throws Exception {
    boolean validate = false;
    validate = true;/*ww  w . jav a  2s.  c  o  m*/

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(validate);

    XMLReader reader = null;
    SAXParser parser = spf.newSAXParser();
    reader = parser.getXMLReader();

    reader.setErrorHandler(new MyErrorHandler());
    reader.parse("http://yourURL/TextDoc3.xml");
}

From source file:MainClass.java

static public void main(String[] arg) throws Exception {
    boolean validate = false;

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(validate);/*from   w w  w .j a  va2s.  co  m*/

    XMLReader reader = null;
    SAXParser parser = spf.newSAXParser();
    reader = parser.getXMLReader();

    reader.setErrorHandler(new MyErrorHandler());
    reader.parse(new InputSource(new StringReader(xmlString)));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String parserClass = "org.apache.xerces.parsers.SAXParser";
    String validationFeature = "http://xml.org/sax/features/validation";
    String schemaFeature = "http://apache.org/xml/features/validation/schema";
    String x = args[0];// ww w . j a v  a 2s.co m
    XMLReader r = XMLReaderFactory.createXMLReader(parserClass);
    r.setFeature(validationFeature, true);
    r.setFeature(schemaFeature, true);
    r.setErrorHandler(new MyErrorHandler());
    r.parse(x);
}

From source file:MyErrorHandler.java

static public void main(String[] arg) throws Exception {
    boolean validate = false;
    validate = true;//from   ww w. j av a 2  s .  c  om

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(validate);

    XMLReader reader = null;
    SAXParser parser = spf.newSAXParser();
    reader = parser.getXMLReader();

    reader.setErrorHandler(new MyErrorHandler());
    InputSource is = new InputSource("test.xml");
    reader.parse(is);
}

From source file:MyErrorHandler.java

static public void main(String[] arg) throws Exception {
    boolean validate = false;
    validate = true;//from  ww w  . j  a  v  a 2  s . c om

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(validate);

    XMLReader reader = null;
    SAXParser parser = spf.newSAXParser();
    reader = parser.getXMLReader();

    reader.setErrorHandler(new MyErrorHandler());
    InputSource is = new InputSource(new StringReader(getXMLData()));
    reader.parse(is);
}

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);/*from w w w . j av  a2  s . c  o  m*/

        // 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:TestModelBuilder.java

public static void main(String[] args) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();
    XMLReader parser = saxParser.getXMLReader();
    SAXModelBuilder mb = new SAXModelBuilder();
    parser.setContentHandler(mb);//from   w  w  w .j a v a2 s.c o  m

    parser.parse(new InputSource("zooinventory.xml"));
    Element2 inventory = (Element2) mb.getModel();
    System.out.println("Animals = " + inventory.getAnimals());
    Element3 cocoa = (Element3) (inventory.getAnimals().get(1));
    ElementA recipe = cocoa.getFoodRecipe();
    System.out.println("Recipe = " + recipe);
}

From source file:SAXCopy.java

static public void main(String[] arg) {
    try {/*ww  w .j a  va  2s  .c o  m*/
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser parser = spf.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setErrorHandler(new MyErrorHandler());
        MyCopyHandler duper = new MyCopyHandler();
        reader.setContentHandler(duper);
        InputSource is = new InputSource("test.xml");
        reader.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (ParserConfigurationException e) {
        System.err.println(e);
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}