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

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

From source file:Main.java

static public void main(String[] arg) throws Exception {
    String filename = "yourXML.xml";
    // Create a new factory that will create the parser.
    SAXParserFactory spf = SAXParserFactory.newInstance();

    // Create the XMLReader to be used to parse the document.
    SAXParser parser = spf.newSAXParser();
    XMLReader reader = parser.getXMLReader();

    // Specify the error handler and the content handler.
    reader.setErrorHandler(new MyErrorHandler());
    reader.setContentHandler(new MyContentHandler());
    // Use the XMLReader to parse the entire file.
    InputSource is = new InputSource(filename);
    reader.parse(is);
}

From source file:SAXCopy.java

static public void main(String[] arg) {
    String infilename = null;//from  ww w. j  ava2s.c  o  m
    String outfilename = null;
    if (arg.length == 2) {
        infilename = arg[0];
        outfilename = arg[1];
    } else {
        usage();
    }

    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser parser = spf.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        reader.setErrorHandler(new MyErrorHandler());
        FileOutputStream fos = new FileOutputStream(outfilename);
        PrintWriter out = new PrintWriter(fos);
        MyCopyHandler duper = new MyCopyHandler(out);
        reader.setContentHandler(duper);
        InputSource is = new InputSource(infilename);
        reader.parse(is);
        out.close();
    } 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);
    }
}

From source file:MyContentHandler.java

static public void main(String[] arg) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader reader = null;
    try {/*from  w w w  . ja v a  2 s. c  o  m*/
        SAXParser parser = spf.newSAXParser();
        reader = parser.getXMLReader();
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
    }

    reader.setErrorHandler(new MyErrorHandler());
    reader.setContentHandler(new MyContentHandler());

    try {
        InputSource is = new InputSource("test.xml");
        reader.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:SAXCheck.java

static public void main(String[] arg) {
    String filename = null;/*from  www . j  av a  2 s. c o m*/
    boolean validate = false;

    if (arg.length == 1) {
        filename = arg[0];
    } else if (arg.length == 2) {
        if (!arg[0].equals("-v"))
            usage();
        validate = true;
        filename = arg[1];
    } else {
        usage();
    }

    // Create a new factory to create parsers that will
    // validate or not, according to the flag setting.
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(validate);

    // Create the XMLReader to be used to check for errors.
    XMLReader reader = null;
    try {
        SAXParser parser = spf.newSAXParser();
        reader = parser.getXMLReader();
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
    }

    // Install an error handler in the reader.
    reader.setErrorHandler(new MyErrorHandler());
    // Use the XMLReader to parse the entire file.
    try {
        InputSource is = new InputSource(filename);
        reader.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:MyContentHandler.java

static public void main(String[] arg) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader reader = null;
    try {// w w w  .  ja v  a2  s.  com
        SAXParser parser = spf.newSAXParser();
        reader = parser.getXMLReader();
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
    }

    reader.setErrorHandler(new MyErrorHandler());
    reader.setContentHandler(new MyContentHandler());

    try {
        InputSource is = new InputSource(new StringReader(getXMLData()));
        reader.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader reader = null;
    SAXParser parser = spf.newSAXParser();
    reader = parser.getXMLReader();/*from  w  w w.  ja  v a2 s.c om*/
    reader.setErrorHandler(new MyErrorHandler());
    reader.setContentHandler(new MyTextHandler());

    StringReader sr = new StringReader(
            "<folks><person><phone>502 555-2192</phone><name>B, M</name></person></folks>");

    //InputSource is = new InputSource("xmlFileName.xml");
    InputSource is = new InputSource(sr);
    reader.parse(is);
}

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");
    }/* w  w  w . ja va 2s  .c  o m*/
}

From source file:Main.java

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

    XMLReader reader = XMLReaderFactory.createXMLReader();
    reader.setContentHandler(handler);//w w w .j av  a 2s. c  om
    reader.parse(new InputSource(in));
    in.close();
    return reader;
}

From source file:Main.java

/**
 * Parse the XML data in the given input stream, using the
 * specified handler object as both the content and error handler.
 *
 * @param handler the SAX event handler/*from  w w  w .ja  v  a  2s . com*/
 * @param in the input stream containing the XML to be parsed
 */
public static void parse(DefaultHandler handler, InputStream in)
        throws IOException, ParserConfigurationException, SAXException {
    XMLReader xr = _pfactory.newSAXParser().getXMLReader();

    xr.setContentHandler(handler);
    xr.setErrorHandler(handler);

    xr.parse(new InputSource(in));
}