Last active
August 29, 2015 14:01
-
-
Save gvsharma/7ff2acf2a621fd0e24ce to your computer and use it in GitHub Desktop.
A java class very useful for SAX parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.StringReader; | |
import java.net.MalformedURLException; | |
import java.net.URL; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import javax.xml.parsers.SAXParser; | |
import javax.xml.parsers.SAXParserFactory; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Element; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.xml.sax.ContentHandler; | |
import org.xml.sax.InputSource; | |
import org.xml.sax.SAXException; | |
import org.xml.sax.XMLReader; | |
import android.app.Activity; | |
import android.app.AlertDialog; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.os.AsyncTask; | |
public class XMLParser { | |
static AlertDialog dialog ; | |
public static void saxParser(final Activity context, final String url,final ServiceResponseListener serviceResponseListener, final ContentHandler handler) { | |
new AsyncTask<Void, Void, Void>() { | |
private boolean response; | |
private int iterate; | |
@Override | |
protected void onPreExecute() { | |
serviceResponseListener.showLoadingIcon(); | |
} | |
@Override | |
protected Void doInBackground(Void... params) { | |
do { | |
iterate++; | |
try { | |
SAXParserFactory spf = SAXParserFactory.newInstance(); | |
SAXParser sp = spf.newSAXParser(); | |
XMLReader myReader = sp.getXMLReader(); | |
myReader.setContentHandler(handler); | |
InputStream is = new URL(url).openStream(); | |
InputSource inputSource = new InputSource(is); | |
inputSource.setEncoding("ISO-8859-1"); | |
myReader.parse(inputSource); | |
response = true; | |
} catch (SAXException e) { | |
e.printStackTrace(); | |
} catch (MalformedURLException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} catch (ParserConfigurationException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} while(!response && (iterate <= 3)); | |
return null; | |
} | |
@Override | |
protected void onPostExecute(Void result) { | |
serviceResponseListener.hideLoadingIcon(); | |
serviceResponseListener.loadData(null); | |
} | |
}.execute(); | |
} | |
public static Bitmap getBitmapFromURL(final String url) { | |
final int BUFFER_IO_SIZE = 8000; | |
try { | |
BufferedInputStream bis = new BufferedInputStream(new URL(url).openStream(), BUFFER_IO_SIZE); | |
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
BufferedOutputStream bos = new BufferedOutputStream(baos, BUFFER_IO_SIZE); | |
copy(bis, bos); | |
bos.flush(); | |
return BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.size()); | |
} catch (IOException e) { | |
return null; | |
} | |
} | |
private static void copy(final InputStream bis, final OutputStream baos) throws IOException { | |
byte[] buf = new byte[256]; | |
int l; | |
while ((l = bis.read(buf)) >= 0) baos.write(buf, 0, l); | |
} | |
public static Document XMLfromString(String xml){ | |
Document doc = null; | |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
try { | |
DocumentBuilder db = dbf.newDocumentBuilder(); | |
InputSource is = new InputSource(); | |
is.setCharacterStream(new StringReader(xml)); | |
doc = db.parse(is); | |
} catch (ParserConfigurationException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (SAXException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (IOException e) { | |
e.printStackTrace(); | |
return null; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
return null; | |
} | |
return doc; | |
} | |
public static String getValue(Element item, String str) { | |
if(item==null) { | |
return ""; | |
} else { | |
NodeList n = item.getElementsByTagName(str); | |
return getElementValue(n.item(0)); | |
} | |
} | |
public static final String getElementValue(Node element) { | |
Node kid; | |
if(element != null) { | |
if(element.hasChildNodes()) { | |
StringBuffer strNodeValue = new StringBuffer(); | |
for(kid = element.getFirstChild(); kid != null; kid = kid.getNextSibling()) { | |
if(kid.getNodeType() == Node.TEXT_NODE || kid.getNodeType() == Node.CDATA_SECTION_NODE) { | |
strNodeValue.append(kid.getNodeValue()); | |
} | |
} | |
return strNodeValue.toString().trim(); | |
} | |
} | |
return ""; | |
} | |
} | |
public abstract class ServiceResponseListener { | |
public abstract void loadData(Object resp); | |
public void showLoadingIcon(){}; | |
public void hideLoadingIcon(){}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment