List of usage examples for javax.xml.parsers SAXParser getXMLReader
public abstract org.xml.sax.XMLReader getXMLReader() throws SAXException;
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 ww w . ja v a 2 s . c o m }
From source file:Main.java
public static void main(String args[]) throws Exception { SAXParserFactory spf = SAXParserFactory.newInstance(); XMLReader reader = null;/*from ww w. j a v a2s.c om*/ 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:SAXCopy.java
static public void main(String[] arg) { String infilename = null;/*ww w .j ava2 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:MappingContentHandler.java
static public void main(String[] arg) { try {/*from w w w . ja v a 2 s . 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: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);//w w w.j av a 2 s. 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:Main.java
public static boolean validateWithDTDUsingSAX(String xml) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true);/* w w w.java 2 s .c o m*/ factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new ErrorHandler() { public void warning(SAXParseException e) throws SAXException { System.out.println("WARNING : " + e.getMessage()); // do nothing } public void error(SAXParseException e) throws SAXException { System.out.println("ERROR : " + e.getMessage()); throw e; } public void fatalError(SAXParseException e) throws SAXException { System.out.println("FATAL : " + e.getMessage()); throw e; } }); reader.parse(new InputSource(xml)); return true; }
From source file:Main.java
public static XMLReader createXMLReader() { SAXParserFactory factory = createParserFactory(); try {// w ww. j av a 2 s .co m SAXParser parser = factory.newSAXParser(); return parser.getXMLReader(); } catch (ParserConfigurationException ex) { throw new IllegalStateException(ex); } catch (SAXException ex) { throw new IllegalStateException(ex); } }
From source file:Main.java
public static synchronized SAXParser getSAXParser() { if (!stack.empty()) return (SAXParser) stack.pop(); try {//www . j av a 2 s . com SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setProperty("http://apache.org/xml/properties/input-buffer-size", new Integer(8192)); return parser; } catch (Exception exc) { System.err.println("Error attempting to initialize SAXParserFactory: " + exc); exc.printStackTrace(); return null; } }
From source file:net.eledge.android.europeana.tools.RssReader.java
public static List<BlogArticle> readFeed(String url, DateTime lastViewed) { InputStream is = null;/*www .j a v a 2 s. co m*/ try { HttpGet request = new HttpGet(url); AndroidHttpClient.modifyRequestToAcceptGzipResponse(request); HttpResponse response = new DefaultHttpClient().execute(request); is = AndroidHttpClient.getUngzippedContent(response.getEntity()); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); RssFeedHandler rh = new RssFeedHandler(lastViewed); xr.setContentHandler(rh); xr.parse(new InputSource(is)); return rh.articles; } catch (IOException | SAXException | ParserConfigurationException e) { Log.e("RssReader", e.getMessage(), e); } finally { IOUtils.closeQuietly(is); } return null; }
From source file:com.mindquarry.desktop.I18N.java
protected static Map<String, String> initTranslationMap(String fileBase, String fileSuffix) { try {//from w w w. ja v a 2 s . c om SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setValidating(false); SAXParser parser = parserFactory.newSAXParser(); XMLReader reader = parser.getXMLReader(); TranslationMessageParser translationParser = new TranslationMessageParser(); reader.setContentHandler(translationParser); reader.setErrorHandler(translationParser); // TODO: use "xx_YY" if available, use "xx" otherwise: String transFile = fileBase + Locale.getDefault().getLanguage() + fileSuffix; InputStream is = I18N.class.getResourceAsStream(transFile); if (is == null) { // no translation available for this language log.debug("No translation file available for language: " + Locale.getDefault().getLanguage()); return new HashMap<String, String>(); } log.debug("Loading translation file " + transFile + " from JAR"); reader.parse(new InputSource(is)); return translationParser.getMap(); } catch (Exception e) { throw new RuntimeException(e.toString(), e); } }