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

public static void main(String args[]) {
    try {//from   www . ja  va2s.c  o  m
        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(new InputSource(new StringReader(xmlString)), handler);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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 . j  av a2s .c o  m*/
        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[]) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);//  w w 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 = 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:Main.java

public static void main(String[] args) throws Exception {
    String xml = "<!DOCTYPE html><hml><img/></hml>";
    SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
    InputSource in = new InputSource(new StringReader(xml));

    DefaultHandler2 myHandler = new DefaultHandler2() {
        @Override//  w  w w . j  a  v a2  s .com
        public void startElement(String uri, String localName, String qName, Attributes attributes)
                throws SAXException {
            System.out.println("Element: " + qName);
        }

        @Override
        public void startDTD(String name, String publicId, String systemId) throws SAXException {
            System.out.println("DocType: " + name);
        }
    };
    saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", myHandler);
    saxParser.parse(in, myHandler);
}

From source file:MainClass.java

static public void main(String[] arg) throws Exception {
    boolean validate = false;

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(validate);// w ww  .  j  a  va  2  s.  com

    XMLReader reader = null;
    SAXParser parser = spf.newSAXParser();
    reader = parser.getXMLReader();

    reader.setErrorHandler(new MyErrorHandler());
    reader.parse(new InputSource(new StringReader(xmlString)));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);//from   www.j a  va2 s.  co  m
    SAXParser parser = factory.newSAXParser();
    parser.parse(new InputSource(new StringReader(getXMLData())), new SampleOfXmlLocator());
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);//from  w w  w .  ja  v  a2  s  .com
    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 ww.  ja v a2s .c om
  SAXParser parser = factory.newSAXParser();
  SaxHandler handler = new SaxHandler();
  parser.parse("sample.xml", handler);
}

From source file:XMLTransform.java

public static void main(String[] args) throws Exception {
    System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
    SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
    XMLReader reader = parser.getXMLReader();
    TransformerFactory factory = TransformerFactory.newInstance();
    System.out.println(factory);//from   w  w  w .  j a v a  2  s  .c  om
    Transformer transformer = factory.newTransformer(new StreamSource("./xsl/books-sql.xsl"));
    transformer.setParameter("user", "root");
    transformer.setParameter("password", "123456");
    transformer.transform(new StreamSource("./xml/books.xml"), new StreamResult(System.out));
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    String language = XMLConstants.W3C_XML_SCHEMA_NS_URI;
    SchemaFactory factory = SchemaFactory.newInstance(language);
    Schema schema = factory.newSchema(new File("yourSchema"));

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setSchema(schema);//from   w w  w.  ja  v a  2 s.com

    SAXParser parser = spf.newSAXParser();

    // parser.parse(...);
}