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[] argv) throws Exception {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  factory.setValidating(true);/*from w ww  .ja  va  2 s . c o m*/
  SAXParser parser = factory.newSAXParser();
  SaxHandler handler = new SaxHandler();
  parser.parse("sample.xml", handler);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);//w  w  w  .j  a  va  2 s  .c o  m
    SAXParser parser = factory.newSAXParser();
    SaxHandler handler = new SaxHandler();
    parser.parse("sample.xml", handler);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);//from  ww  w .  ja v a 2s.  c om
    SAXParser parser = factory.newSAXParser();
    parser.parse(new InputSource(new StringReader(getXMLData())), new SampleOfXmlLocator());
}

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 .  j av  a2s  . 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:MyErrorHandler.java

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

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

public static void main(String[] args) throws Exception {
    String url;/*  w ww .  java 2s  .  com*/
    if (args.length == 0) {
        url = "http://www.w3c.org";
        System.out.println("Using " + url);
    } else
        url = args[0];

    DefaultHandler handler = new DefaultHandler() {
        public void startElement(String namespaceURI, String lname, String qname, Attributes attrs) {
            if (lname.equals("a") && attrs != null) {
                for (int i = 0; i < attrs.getLength(); i++) {
                    String aname = attrs.getLocalName(i);
                    if (aname.equals("href"))
                        System.out.println(attrs.getValue(i));
                }
            }
        }
    };

    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);
    SAXParser saxParser = factory.newSAXParser();
    InputStream in = new URL(url).openStream();
    saxParser.parse(in, handler);
}

From source file:MyErrorHandler.java

static public void main(String[] arg) throws Exception {
    boolean validate = false;
    validate = true;/* www .ja v a 2 s.co m*/

    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: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 v a2s.  c  o 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:MyErrorHandler.java

static public void main(String[] arg) throws Exception {
    boolean validate = false;
    validate = true;// w  w w.j a va 2s  .com

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

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