package com.practice.main;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import android.app.Activity;
import android.os.Bundle;
import android.sax.Element;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class WebActivity extends Activity
{
private HttpGet mRequest;
private HttpClient mClient;
private BufferedReader mReader;
private DocumentBuilder mBuilder;
private StringBuffer mBuffer;
private String mNewLine;
TextView source;
TextView result;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.web_activity);
source = (TextView) findViewById(R.id.source);
result = (TextView) findViewById(R.id.result);
@Override
public void onClick(View view)
{
String url = "https://www.google.com.bd/"; // Random site with XML data
String[] tags = new String[] { "ARTIST" };
String code = "";
String parsed = "";
try
{
WebActivity mGrabber;
code = mGrabber.grabSource(url);
parsed = mGrabber.parseTags(code, tags);
}
catch (Exception e)
{
e.printStackTrace();
return;
}
TextView source = (TextView) findViewById(R.id.source);
source.setText(code);
TextView result = (TextView) findViewById(R.id.result);
result.setText(parsed);
}
}
public void SrcGrabber()
{
mRequest = new HttpGet();
mClient = new DefaultHttpClient();
mReader = null;
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
mBuilder = factory.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
}
mBuffer = new StringBuffer(2000);
mNewLine = System.getProperty("line.separator");
}
public String grabSource(String url) throws ClientProtocolException, IOException, URISyntaxException
{
mBuffer.setLength(0);
try
{
mRequest.setURI(new URI(url));
HttpResponse response = mClient.execute(mRequest);
mReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = mReader.readLine()) != null)
{
mBuffer.append(line);
mBuffer.append(mNewLine);
}
}
finally
{
closeReader();
}
return mBuffer.toString();
}
public String parseTags(String code, String[] tags) throws IOException, SAXException
{
mBuffer.setLength(0);
Document doc = mBuilder.parse(new ByteArrayInputStream(code.getBytes()));
Element el = (Element) doc.getDocumentElement();
for (String tag : tags)
parseTags(((Document) el).getElementsByTagName(tag));
return mBuffer.toString();
}
protected void parseTag(Node node)
{
if (node.getNodeType() == Node.TEXT_NODE)
{
mBuffer.append(node.getNodeValue());
mBuffer.append(mNewLine);
}
}
private void closeReader()
{
if (mReader == null)
return;
try
{
mReader.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
private void parseTags(NodeList nodes)
{
int length = nodes.getLength();
for (int i = 0; i < length; ++i)
parseTag(nodes.item(i).getFirstChild());
}
}