Example usage for javax.xml.parsers SAXParser parse

List of usage examples for javax.xml.parsers SAXParser parse

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParser parse.

Prototype

public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException 

Source Link

Document

Parse the content given org.xml.sax.InputSource as XML using the specified org.xml.sax.helpers.DefaultHandler .

Usage

From source file:Main.java

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

From source file:Main.java

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

From source file:Main.java

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    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 {
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    parser.parse(new File("test.xml"), new DefaultHandler() {

        @Override/*from w  ww  .  j a  va2 s . co m*/
        public void startElement(String uri, String localName, String qName, Attributes atts)
                throws SAXException {
            if (qName.equals("passenger")) {
                System.out.println("id = " + atts.getValue(0));
            }
        }

        @Override
        public void endElement(String uri, String localName, String qName) throws SAXException {
        }

        @Override
        public void characters(char[] ch, int start, int length) throws SAXException {
            String text = new String(ch, start, length);
            if (!text.trim().isEmpty()) {
                System.out.println("name " + text);
            }
        }
    });
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    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);/*from w  w w  .j a  v  a2  s .  co  m*/
    SAXParser parser = factory.newSAXParser();
    SaxHandler handler = new SaxHandler();
    parser.parse(new InputSource(new StringReader(getXMLData())), handler);
}

From source file:Main.java

  public static void main(String[] argv) throws Exception {
  SAXParserFactory factory = SAXParserFactory.newInstance();
  factory.setValidating(true);//from   w  w  w .  j  a  v a2 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);//from  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:SAXTest.java

public static void main(String[] args) throws Exception {
    String url;//from   w w w .  ja v  a2s  . co m
    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);
}