XML Parser Example

1 vote · 0 comments

Using the Handler from XML Parser Class we can parse XML very easily.

raw ·
copy
· download
public class TestParser { //Vars to save the info private String text; private String number; private MyParser myparser; public void fetchInfo(String urls) { try { URL url = new URL(urls); SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); XMLReader xr = sp.getXMLReader(); myparser = new MyParser(); xr.setContentHandler(myparser); Log.i("ExampleParse", "Parsing starting"); xr.parse(new InputSource(url.openStream())); Log.i("ExampleParse", "Parsing done"); //Get parsed info text=myparser.getText(); number=myparser.getNumber(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

Be the first to comment

Sign in with OpenID