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: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);/*www.j av  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:SAXCheck.java

static public void main(String[] arg) {
    String filename = null;/*w  w w .  j  a va2 s  .  co  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:MainClass.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/*from   ww w  .j  a va  2  s  . c om*/
    spf.setValidating(true);
    System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
    System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");

    SAXParser parser = spf.newSAXParser();
    System.out.println("Parser object is: " + parser);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser parser = null;/*from  w  ww  . j a  va2s . c  o  m*/
    spf.setNamespaceAware(true);
    spf.setValidating(true);
    try {
        spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        parser = spf.newSAXParser();
        System.out.println("Parser object is: " + parser);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }

    MySAXHandler handler = new MySAXHandler();
    parser.parse(new InputSource(new StringReader(xmlString)), handler);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser parser = null;/*from  www  .  ja  va 2  s. co m*/
    spf.setNamespaceAware(true);
    spf.setValidating(true);
    try {
        spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        parser = spf.newSAXParser();
        System.out.println("Parser object is: " + parser);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    } catch (ParserConfigurationException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }

    MySAXHandler handler = new MySAXHandler();
    parser.parse(new InputSource(new StringReader(xmlString)), handler);
}

From source file:MainClass.java

public static void main(String args[]) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);//from  ww w.j  av  a  2  s. c o  m
    spf.setValidating(true);
    System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
    System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");

    SAXParser parser = null;
    try {
        parser = spf.newSAXParser();
    } catch (ParserConfigurationException e) {
        e.printStackTrace(System.err);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
    }
    System.out.println("Parser object is: " + parser);
}

From source file:TrySAXHandler.java

public static void main(String args[]) throws Exception {
        File file = new File("y.xml");
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser parser = null;//w  ww  .jav a2s  .  com
        spf.setNamespaceAware(true);
        spf.setValidating(true);
        System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
        System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");

        parser = spf.newSAXParser();
        System.out.println("Parser object is: " + parser);

        MySAXHandler handler = new MySAXHandler();
        parser.parse(file, handler);
    }

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser parser = null;// w ww. java 2 s  .  c  o  m
    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:SAXDemo.java

/** The main method sets things up for parsing */
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
    // Create a JAXP "parser factory" for creating SAX parsers
    javax.xml.parsers.SAXParserFactory spf = SAXParserFactory.newInstance();

    // Configure the parser factory for the type of parsers we require
    spf.setValidating(false); // No validation required

    // Now use the parser factory to create a SAXParser object
    // Note that SAXParser is a JAXP class, not a SAX class
    javax.xml.parsers.SAXParser sp = spf.newSAXParser();

    // Create a SAX input source for the file argument
    org.xml.sax.InputSource input = new InputSource(new FileReader(args[0]));

    // Give the InputSource an absolute URL for the file, so that
    // it can resolve relative URLs in a <!DOCTYPE> declaration, e.g.
    input.setSystemId("file://" + new File(args[0]).getAbsolutePath());

    // Create an instance of this class; it defines all the handler methods
    SAXDemo handler = new SAXDemo();

    // Finally, tell the parser to parse the input and notify the handler
    sp.parse(input, handler);//from   w ww  .  j a v  a2  s  .  c o m

    // Instead of using the SAXParser.parse() method, which is part of the
    // JAXP API, we could also use the SAX1 API directly. Note the
    // difference between the JAXP class javax.xml.parsers.SAXParser and
    // the SAX1 class org.xml.sax.Parser
    //
    // org.xml.sax.Parser parser = sp.getParser(); // Get the SAX parser
    // parser.setDocumentHandler(handler); // Set main handler
    // parser.setErrorHandler(handler); // Set error handler
    // parser.parse(input); // Parse!
}

From source file:it.wami.map.mongodeploy.OsmToMongoDB.java

/**
 * @param args//from w  w  w .  ja v a  2  s. c o m
 */
public static void main(String[] args) {

    if (args == null || args.length == 0 || args.length < 2) {
        System.out.println(Options.USAGE);
        return;
    }

    parseCommandOptions(args);

    MongoClientOptions mco = new MongoClientOptions.Builder().connectionsPerHost(150000)
            .threadsAllowedToBlockForConnectionMultiplier(10000).build();

    MongoClient mongoClient = createMongo(options.getHost(), options.getPort(), mco);

    DB db = mongoClient.getDB(options.getDbName());
    DBCollection nodes = db.createCollection(OsmSaxHandler.COLL_NODES, null);
    createNodesIndex(nodes);
    /*DBCollection ways = */
    DBCollection ways = db.createCollection(OsmSaxHandler.COLL_WAYS, null);
    createWaysIndex(ways);
    /*DBCollection relations = */
    DBCollection relations = db.createCollection(OsmSaxHandler.COLL_RELATIONS, null);
    createRelationsIndex(relations);
    db.createCollection(OsmSaxHandler.COLL_TAGS, null);

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = null;
    try {
        saxParser = factory.newSAXParser();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    }

    DefaultHandler handler = new OsmSaxHandler(db, options);

    try {
        if (options.getInput().contains(".bz2")) {
            try {
                saxParser.parse(getInputStreamForBZ2File(options.getInput()), handler);
            } catch (CompressorException e) {
                e.printStackTrace();
            }
        } else if (options.getInput().contains(".osm")) {
            saxParser.parse(options.getInput(), handler);
        } else {
            throw new IllegalArgumentException("input must be an osm file or a bz2.");
        }
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}