List of usage examples for javax.xml.parsers SAXParser parse
public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException
From source file:Main.java
public static void main(String[] args) throws Exception { String xml = "<?xml version='1.0'?><test><test2></test2></test>"; String schemaString = // "<?xml version='1.0'?>"// + "<xsd:schema xmlns:xsd='http://www.w3.org/2001/XMLSchema' elementFormDefault='unqualified' attributeFormDefault='unqualified'>"// + "<xsd:element name='test' type='Test'/>"// + "<xsd:element name='test2' type='Test2'/>"// + "<xsd:complexType name='Test'>"// + "<xsd:sequence>"// + "<xsd:element ref='test2' minOccurs='1' maxOccurs='unbounded'/>"// + "</xsd:sequence>"// + "</xsd:complexType>"// + "<xsd:simpleType name='Test2'>"// + "<xsd:restriction base='xsd:string'><xsd:minLength value='1'/></xsd:restriction>"// + "</xsd:simpleType>"// + "</xsd:schema>"; Source schemaSource = new StreamSource(new StringReader(schemaString)); Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaSource); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true);//from w ww . j av a 2 s .c o m factory.setSchema(schema); SAXParser parser = factory.newSAXParser(); MyContentHandler handler = new MyContentHandler(); parser.parse(new InputSource(new StringReader(xml)), handler); }
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); SAXBooks books = handler.getBooks(); for (int i = 0; i < books.getBookSize(); i++) { SAXBook book = books.getBook(i); System.out.println(book); }//from w w w .jav a2s . c o m }
From source file:net.semanticmetadata.lire.solr.SearchImages.java
public static void main(String[] args) throws IOException, ParserConfigurationException, SAXException { // http://localhost:8080/solr/lire/query?q=hashes%3A1152++hashes%3A605++hashes%3A96++hashes%3A275++&wt=xml String hashes = "1152 605 96 275 2057 3579 3950 2831 2367 3169 3292 974 2465 1573 2933 3125 314 2158 3532 974 2198 2315 3013 3302 3316 1467 2213 818 3 1083 18 2604 327 1370 593 3677 464 79 256 984 2496 1124 855 2091 780 1941 1887 1145 1396 4016 2406 2227 1532 2598 215 1375 171 2516 1698 368 2350 3799 223 1471 2083 1051 3015 3789 3374 1442 3991 3575 1452 751 428 3103 1182 2241 474 275 3678 3970 559 3394 2662 2361 2048 1083 181 1483 3903 3331 2363 756 558 2838 3984 1878 2667 3333 1473 2136 3499 3873 1437 3091 1287 948 46 3660 3003 1572 1185 2231 2622 257 3538 3632 3989 1180 3928 3144 1492 3941 3253 3498 2721 1036 22 1020 725 1431 3821 2248 2542 3659 2849 524 2967 1 2493 3620 2951 3584 1641 3873 2087 1506 1489 3064"; String[] split = hashes.split(" "); String query = ""; for (int i = 0; i < split.length; i++) { String s = split[i];/*from w w w. j a v a 2s . c om*/ if (s.trim().length() > 0) query += " cl_ha:" + s.trim(); } URL u = new URL(baseURL + "?q=" + URLEncoder.encode(query, "utf-8") + "&wt=xml&rows=500"); InputStream in = u.openStream(); SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); SolrResponseHandler dh = new SolrResponseHandler(); saxParser.parse(in, dh); ArrayList<ResultItem> results = dh.getResults(); // re-rank: }
From source file:ca.uqac.info.tag.Counter.TagCounter.java
public static void main(final String[] args) { // Parse command line arguments Options options = setupOptions();/*www .ja va 2 s . co m*/ CommandLine c_line = setupCommandLine(args, options); String redirectionFile = ""; String tagAnalyse = ""; String inputFile = ""; if (c_line.hasOption("h")) { showUsage(options); System.exit(ERR_OK); } //Contains a redirection file for the output if (c_line.hasOption("redirection")) { try { redirectionFile = c_line.getOptionValue("redirection"); PrintStream ps; ps = new PrintStream(redirectionFile); System.setOut(ps); } catch (FileNotFoundException e) { System.out.println("Redirection error !!!"); e.printStackTrace(); } } //Contains a tag if (c_line.hasOption("Tag")) { tagAnalyse = c_line.getOptionValue("t"); } else { System.err.println("No Tag in Arguments"); System.exit(ERR_ARGUMENTS); } //Contains a InputFile if (c_line.hasOption("InputFile")) { inputFile = c_line.getOptionValue("i"); } else { System.err.println("No Input File in Arguments"); System.exit(ERR_ARGUMENTS); } //Start of the program System.out.println("-----------------------------------------------"); System.out.println("The count of the Tag is start !!!"); // Throw the Sax parsing for the file try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser; parser = factory.newSAXParser(); File Tagfile = new File(inputFile); DefaultHandler manager = new SaxTagHandlers(tagAnalyse); parser.parse(Tagfile, manager); } catch (ParserConfigurationException e) { System.out.println("Parser Configuration Exception for Sax !!!"); e.printStackTrace(); } catch (SAXException e) { System.out.println("Sax Exception during the parsing !!!"); e.printStackTrace(); } catch (IOException e) { System.out.println("Input/Ouput Exception during the Sax parsing !!!"); e.printStackTrace(); } }
From source file:Main.java
public static void main(String args[]) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println(qName); }/*from ww w. j a v a2 s . co m*/ public void characters(char ch[], int start, int length) throws SAXException { System.out.println(new String(ch, start, length)); } }; saxParser.parse(args[0], handler); }
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; spf.setNamespaceAware(true);//from w w w . ja 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"); 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; spf.setNamespaceAware(true);/*from w ww .j a v a 2 s. c o m*/ 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; spf.setNamespaceAware(true);/* w w w . j ava2 s . c om*/ 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: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/*from ww w . j ava 2 s . c o m*/ 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: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); // 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 ///*from w ww . j av a 2s .com*/ // 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! }