Back to top

Android XML parser

URL url = new URL("http://craftbeerguild.net/mobile.json?barcode="+contents);
//URL url = new URL("http://www.anddev.org/images/tut/basic/parsingxml/example.xml");
/* Get a SAXParser from the SAXPArserFactory */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();

/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();

/* Create a new ContentHandler and apply it to the XML-Reader */
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
Log.v("debugging","Hello World");
/* Parse the xml-data from our URL */
InputStream mystream = url.openStream();
InputSource is = new InputSource(mystream);
xr.parse(is);
/* Parsing has finished. */

/* Our ExampleHandler now provides the parsed data to us.*/
ParsedExampleDataSet parsedExampleDataSet = myExampleHandler.getParsedData();

tv.setText(parsedExampleDataSet.toString());
tv.setText("HEllo WOrld:"+is.toString());

public class ParsedExampleDataSet {
private String extractedString =null;
private int extractedInt =0;

public String getExtractedString(){
return extractedString;
}

public void setExtractedString(String extractedString){
this.extractedString += extractedString;
}

public int getExtractedInt(){
return extractedInt;
}

public void setExtractedInt(int extractedInt){
this.extractedInt += extractedInt;
}

public String toString(){
return "ExtractedString = "+this.extractedString+"\nExtractedInt = "+this.extractedInt;
}

}

public class ExampleHandler extends DefaultHandler{
//===================================================================================
// Fields
//===================================================================================
private boolean in_title = false;
private boolean in_link = false;
private boolean in_mytag = false;
private boolean in_description = false;

private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();

@Override
public void startElement(String namespacesURI, String localName, String qName, Attributes atts) throws SAXException{
if (localName.equals("title")){
this.in_title = true;
}else if(localName.equals("link")){
this.in_link = true;
}else if (localName.equals("description")){
this.in_description=true;
}else if (localName.equals("mytag")){
this.in_mytag = true;
}
}

@Override
public void endElement(String namespaceURL, String localName, String qName)throws SAXException{
if (localName.equals("title")){
this.in_title = false;
}else if (localName.equals("link")){
this.in_link = false;
}else if (localName.equals("description")){
this.in_description = false;
}else if (localName.equals("mytag")){
this.in_mytag = false;
}
}

@Override
public void characters(char ch[], int start, int length){
if(this.in_description){
myParsedExampleDataSet.setExtractedString(new String(ch,start,length));
myParsedExampleDataSet.setExtractedInt(length);
}
}

@Override
public void startDocument() throws SAXException{
// Do some startup if needed
}

@Override
public void endDocument() throws SAXException{
// Do some finishing work if needed
}

public ParsedExampleDataSet getParsedData(){
return this.myParsedExampleDataSet;
}
}

Comments

Add new comment