Example usage for javax.xml.parsers SAXParserFactory newInstance

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

Introduction

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

Prototype


public static SAXParserFactory newInstance() 

Source Link

Document

Obtain a new instance of a SAXParserFactory .

Usage

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  w  ww .j av a  2 s .  c om*/
    SAXBooks books = handler.getBooks();

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

From source file:Main.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    XMLReader reader = null;/*  ww  w  .  j  av a 2 s  .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: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  w w  w  .  j  a v a  2 s .  c om*/
}

From source file:SAXCopy.java

static public void main(String[] arg) {
    try {//  w ww.j a v  a  2s.c  om
        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);
    }
}

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

static public void main(String[] arg) {
    try {//from w ww. j  a v a 2s. c om
        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 factory = SAXParserFactory.newInstance();
    factory.setValidating(true);//  w w w  .  j  av  a  2s  .  c o  m
    SAXParser parser = factory.newSAXParser();
    parser.parse("sample.xml", new Main());
}

From source file:SAXCopy.java

static public void main(String[] arg) {
    String infilename = null;/*from  w  w w. j av a2 s  .  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:NameLister.java

public static void main(String args[]) {

    if (args.length != 1) {
        System.err.println("Usage: java NameLister xmlfile.xml");
        System.exit(-1);// w  w  w  .ja v  a  2 s  .c o m
    }

    try {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        DefaultHandler handler = new DefaultHandler() {
            boolean name = false;

            public void startElement(String uri, String localName, String qName, Attributes attributes)
                    throws SAXException {
                if (qName.equalsIgnoreCase("NAME")) {
                    name = true;
                }
            }

            public void characters(char ch[], int start, int length) throws SAXException {
                if (name) {
                    System.out.println("Name: " + new String(ch, start, length));
                    name = false;
                }
            }
        };

        saxParser.parse(args[0], handler);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:SAXCheck.java

static public void main(String[] arg) {
    String filename = null;/*from   ww  w  .  j  a  va  2  s.  c om*/
    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);
    }
}