Parsing Particular Data From Website In Android
Solution 1:
You can use JSoup parse .
Here you go http://jsoup.org/cookbook/
JSoup is amazing and very effective you will find good example in above link.
First, You have to connect to the webpage you want to parse using:
Document doc = Jsoup.connect("http://example.com/").get();
Make sure you execute above code in non-ui thread using Asynctask or handlers.
Then, you can select page elements using the JSoup selector syntax.
For instance, say you want to select all the content of the div
tags with the id
attribute set to test
, you just have to use:
Elements divs = doc.select("div#test");
to retrieve the divs, then you can iterate on them using:
for (Element div : divs)
System.out.println(div.text());
}
Below is Example Snippet.
package org.jsoup.examples;
import org.jsoup.Jsoup;
import org.jsoup.helper.Validate;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
/**
* Example program to list links from a URL.
*/publicclassListLinks {
publicstaticvoidmain(String[] args)throws IOException {
Validate.isTrue(args.length == 1, "usage: supply url to fetch");
Stringurl= args[0];
print("Fetching %s...", url);
Documentdoc= Jsoup.connect(url).get();
Elementslinks= doc.select("a[href]");
Elementsmedia= doc.select("[src]");
Elementsimports= doc.select("link[href]");
print("\nMedia: (%d)", media.size());
for (Element src : media) {
if (src.tagName().equals("img"))
print(" * %s: <%s> %sx%s (%s)",
src.tagName(), src.attr("abs:src"), src.attr("width"), src.attr("height"),
trim(src.attr("alt"), 20));
else
print(" * %s: <%s>", src.tagName(), src.attr("abs:src"));
}
print("\nImports: (%d)", imports.size());
for (Element link : imports) {
print(" * %s <%s> (%s)", link.tagName(),link.attr("abs:href"), link.attr("rel"));
}
print("\nLinks: (%d)", links.size());
for (Element link : links) {
print(" * a: <%s> (%s)", link.attr("abs:href"), trim(link.text(), 35));
}
}
privatestaticvoidprint(String msg, Object... args) {
System.out.println(String.format(msg, args));
}
privatestatic String trim(String s, int width) {
if (s.length() > width)
return s.substring(0, width-1) + ".";
elsereturn s;
}
}
Last but not list.
If you ever do Asynchronous operation then perform it in Non-UI Thread.
Solution 2:
There are many ways of retrieving data from web. The data could be in form of json string or xml both have different ways of parsing
for xml parsing we can use SAX parser OR DOM parser
to learn more about SAX parser check out this link: try this!!!
Post a Comment for "Parsing Particular Data From Website In Android"