Example usage for javax.xml.parsers SAXParserFactory newSAXParser

List of usage examples for javax.xml.parsers SAXParserFactory newSAXParser

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParserFactory newSAXParser.

Prototype


public abstract SAXParser newSAXParser() throws ParserConfigurationException, SAXException;

Source Link

Document

Creates a new instance of a SAXParser using the currently configured factory parameters.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<?xml version='1.0'?><test><test2></test2></test>";
    String schemaString = //
            "<?xml version='1.0'?>"//
                    + "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' elementFormDefault='unqualified' attributeFormDefault='unqualified'>"//
                    + "<xsd:element name='test' type='Test'/>"//
                    + "<xsd:element name='test2' type='Test2'/>"//
                    + "<xsd:complexType name='Test'>"//
                    + "<xsd:sequence>"//
                    + "<xsd:element ref='test2' minOccurs='1' maxOccurs='unbounded'/>"//
                    + "</xsd:sequence>"//
                    + "</xsd:complexType>"//
                    + "<xsd:simpleType name='Test2'>"//
                    + "<xsd:restriction base='xsd:string'><xsd:minLength value='1'/></xsd:restriction>"//
                    + "</xsd:simpleType>"//
                    + "</xsd:schema>";

    Source schemaSource = new StreamSource(new StringReader(schemaString));
    Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaSource);

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);/*from   w w w  .ja v a 2 s.  co  m*/
    factory.setSchema(schema);
    SAXParser parser = factory.newSAXParser();
    MyContentHandler handler = new MyContentHandler();
    parser.parse(new InputSource(new StringReader(xml)), handler);

}

From source file:SAXCopy.java

static public void main(String[] arg) {
    String infilename = null;//from  w ww .  j a v a 2s  .  co 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:MappingContentHandler.java

static public void main(String[] arg) {
    try {/*from w w w .  j a  v  a  2  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

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader reader = null;// w  ww  .  ja  va 2s  .com
    SAXParser parser = spf.newSAXParser();
    reader = parser.getXMLReader();
    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:MyContentHandler.java

static public void main(String[] arg) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader reader = null;//from  w  w  w  .  java2 s.c o m
    try {
        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:MyContentHandler.java

static public void main(String[] arg) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader reader = null;// www  .ja va 2 s .  c o m
    try {
        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: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  www . j  a  va  2s. c om*/
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
    System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");

    SAXParser parser = null;//from  w  w w. j  a v  a 2 s .com
    parser = spf.newSAXParser();
}

From source file:SAXSample.java

public static void main(String[] args) throws Exception {
    File file = new File("book.xml");
    SAXParserFactory factory = SAXParserFactory.newInstance();

    MyHandler handler = new MyHandler();

    SAXParser saxParser = factory.newSAXParser();
    saxParser.parse(file, handler);//from  ww  w.j a  v  a  2s  . c o m
    SAXBooks books = handler.getBooks();

    for (int i = 0; i < books.getBookSize(); i++) {
        SAXBook book = books.getBook(i);
        System.out.println(book);
    }
}

From source file:ca.uqac.info.tag.Counter.TagCounter.java

public static void main(final String[] args) {
    // Parse command line arguments
    Options options = setupOptions();//  w w w .j a  v  a2  s . c om
    CommandLine c_line = setupCommandLine(args, options);

    String redirectionFile = "";
    String tagAnalyse = "";
    String inputFile = "";

    if (c_line.hasOption("h")) {
        showUsage(options);
        System.exit(ERR_OK);
    }

    //Contains a redirection file for the output
    if (c_line.hasOption("redirection")) {
        try {
            redirectionFile = c_line.getOptionValue("redirection");
            PrintStream ps;
            ps = new PrintStream(redirectionFile);
            System.setOut(ps);
        } catch (FileNotFoundException e) {
            System.out.println("Redirection error !!!");
            e.printStackTrace();
        }
    }

    //Contains a tag
    if (c_line.hasOption("Tag")) {
        tagAnalyse = c_line.getOptionValue("t");
    } else {
        System.err.println("No Tag in Arguments");
        System.exit(ERR_ARGUMENTS);
    }

    //Contains a InputFile 
    if (c_line.hasOption("InputFile")) {
        inputFile = c_line.getOptionValue("i");
    } else {
        System.err.println("No Input File in Arguments");
        System.exit(ERR_ARGUMENTS);
    }

    //Start of the program
    System.out.println("-----------------------------------------------");
    System.out.println("The count of the Tag is start !!!");

    // Throw the Sax parsing for the file 
    try {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser;
        parser = factory.newSAXParser();

        File Tagfile = new File(inputFile);
        DefaultHandler manager = new SaxTagHandlers(tagAnalyse);
        parser.parse(Tagfile, manager);
    } catch (ParserConfigurationException e) {
        System.out.println("Parser Configuration Exception for Sax !!!");
        e.printStackTrace();

    } catch (SAXException e) {
        System.out.println("Sax Exception during the parsing !!!");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("Input/Ouput Exception during the Sax parsing !!!");
        e.printStackTrace();
    }
}