The thing is that I manage to bring a data that I want from the internet but it comes with a tag that I would like to remove. I get the data from here:
<a style="display: block;" class="btn-collapse" onclick="collapseChapter('collapsible490362')" role="button">
<i class="fa fa-chevron-down fa-fw"></i>
Capítulo 120.00
</a>
What interests me from there is the data: Capítulo 120.00. The problem is that in my application this looks like this:
This is how I bring the data:
protected ArrayList<TMODatosSeleccion> doInBackground(Void... voids) {
String url = getIntent().getStringExtra("valor");
tmoDatosSeleccions.clear();
try {
Document doc = Jsoup.connect(url).get();
Elements data = doc.select("div.col-10.text-truncate");
Elements dataDos = doc.select("div.col-2.col-sm-1.text-right");
for (Element e1 : data) {
for(Element e2 : dataDos){
String numeroCap = e1.select("a").html();
String urlManga = e2.select("a").attr("href");
tmoDatosSeleccions.add(new TMODatosSeleccion(numeroCap, urlManga));
}
}
} catch (IOException e) {
e.printStackTrace();
}
return tmoDatosSeleccions;
}
But this in my TextView looks like this:
<i class="fa fa-chevron-down fa-fw"></i> Capítulo 120.00
And like I said, I just want the: Cápitulo 120.00 to be seen
Does anyone know how I can fix it?
To select the text, use String numeroCap = e1.select("a").text() instead of html(). The html() method select everything inside the element.
public static void main(String[] args) {
String html = "<a style=\"display: block;\" class=\"btn-collapse\" onclick=\"collapseChapter('collapsible490362')\" role=\"button\"> <i class=\"fa fa-chevron-down fa-fw\"></i> Capítulo 120.00 </a>";
Document doc = Jsoup.parse(html);
String text = doc.select("a").text();
System.out.print(text);
}
I am trying to match pattern for getting tag and data between two html tag.
to replace data between two tags i want to inspect elements for that Pattern
i want to make pattern regex so i can match it with html elements and reach to that point and replace data between tags.
if anybody know how to create regex pattern for below html tags.
My HTML file is like this:
<div id="frame">
<div class="content">
<div class="messages">
<ul>
<li class="sent">
<img src="http://emilcarlsson.se/assets/mikeross.png" alt="" />
<p>####data</p>
</li>
<li class="replies">
<img src="http://emilcarlsson.se/assets/harveyspecter.png" alt="" />
<p>####data</p>
</li>
</ul>
</div>
</div>
</div>
what i done:
public void readWritedatatFromHtml(){
InputStream input;
try {
input = getResources().openRawResource(R.raw.view);
int size = input.available();
byte[] buffer = new byte[size];
input.read(buffer);
input.close();
String text = new String(buffer);
// Pattern tags = Pattern.compile ("<div class=\"content\">+<div class=\"messages\">+<ul>");
// Pattern tags = Pattern.compile ("<div class=\"content\">\n<div class=\"messages\">");
// Pattern tags = Pattern.compile ("<div class=\"content\">(.*?)<ul>");
Pattern tags = Pattern.compile ("<div class=\"messages\">.? </div>");
Matcher m = tags.matcher(text);
StringBuffer sb = new StringBuffer();
while (m.find()) {
m.appendReplacement(sb, " <ul> <li class=\"sent1\">\n" +
" <img src=\"http://emilcarlsson.se/assets/mikeross.png\" alt=\"\" />\n" +
" <p>####data</p>\n" +
" </li>");
}
m.appendTail(sb);
Log.i("sb",sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Do not under any circumstances try to parse HTML with a regex unless you wish to invoke rite 666 Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn.
Use an HTML parsing library see this page for some ways to do it.
well, after try some pattern i find something like this which is perfectly working for me:
Pattern tags = Pattern.compile ("<div\\s+class=\"messages\">[\\S\\s]*?<\\/div>");
as #JGNI suggested we should avoid this but right now it's right for my requirement if anybody has any better option please guide me so it can helpful to others as well.
I am trying to get only one div (by class) to my webview. I don't know anything about PHP or CSS so i can't realize what should I do when i parse them by class name. I want to take
<div class="container_wrap container_wrap_first main_color fullsize">
part here but its so complicated so i really don't know what to write on doc.select(div. "HERE"). Thanks in advice.
Divs I Must Parse:
<div id="wrap_all">
<div class="mobil-logo">
<div id="main" data-scroll-offset="88">
<!--- header icerik sonu--->
<div class="container_wrap container_wrap_first main_color fullsize">
<div class="container">
And this is what I tried in Main.java:
// webview settings here
loadJsoup();
public void loadJsoup(){
try {
doc = Jsoup.connect("http://isvecehliyet.se/mobil").timeout(10000).get();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element ele = doc.select("div.entry-content-wrapper").first();
String html = ele.toString();
String mime = "text/html";
String encoding = "utf-8";
mWebview.loadData(html, mime, encoding);
}
This works for me:
String url = "http://isvecehliyet.se/mobil/";
Document doc = Jsoup.connect(url).get();
Elements e = doc.select("div.container").first().parents();
System.out.println(e);
Part of output:
<div class="container_wrap container_wrap_first main_color fullsize">
<div class="container">
<main class="template-page content av-content-full alpha units" role="main" itemprop="mainContentOfPage">
<article class="post-entry post-entry [...]
I'm trying to learn jsoup for android and I'm having a hard time with learning the selectors. I've already set up the application with simple buttons and textviews that can retrieve basic info i.e. title etc. Now I'm trying to get the text that I've highlighted below. I've tried multiple times and cannot get the correct syntax down.
<li class="info info info">
<script>clicked = false</script>
<div class="simple">
<p class="name">TEXT I NEED TO PARSE </p>
<ul class="Type">
<li>Normal</li> </ul>
<p class="address">120 Hollywood Blvd.</p>
</div>
<div class="sortables">
<p class="inches"></p>
</div>
<div class="action_links">
</div>
Document doc = null;
try {
doc = Jsoup.connect("http://example.com/index.html").get();
} catch (IOException e) {
// TODO Throws exception
}
Element simple = doc.getElementsByClass("simple").first();
Element p = simple.getElementsByClass("name").first();
Element a = p.select("a").first();
String text = a.text();
System.out.println(text);
I'm loading an html asset page into a WebView using
webMain.loadUrl("file:///android_asset/record.html");
which works fine, but inside the html are a number of places where I'd like to use information from the app. For instance, the HTML may contain text that reads "[Custom]". Is there a way I can replace that word with information passed from the application?
This is an old and already accepted question, however I am sure that the problem can be solved in more elegant way by using javascript.
Keep the html file in your assets folder and surround the text which you want to replace into with div elements with unique id's.
<html>
<head> ... <head>
<body>
Static text
<div id="replace1">replace me</div>
<div id="replace2">replace me too</div>
More static text ...
</body>
</html>
Now create a javascript function which will replace the innerHtml of a div with an id:
function replace(id, newContent)
{
document.getElementById(id).innerHTML = newContent;
}
This function will be best placed directly in the html file, update the <head> section to look like this:
<head>
...
<script type="text/javascript">
function replace(id, newContent)
{
document.getElementById(id).innerHTML = newContent;
}
</script>
</head>
Now we need to call the javascript function from from the WebView Android api:
WebView helpView = (WebView)findViewById(R.id.helpView);
helpView.getSettings().setJavaScriptEnabled(true);
helpView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:replace('replace1', 'new content 1')");
view.loadUrl("javascript:replace('replace2', 'new content 2')");
}
});
helpView.loadUrl("file:///android_asset/help.html");
Using this you will avoid reading potentially large data into memory and running expensive operations on it unnecessarily.
This is worked for me.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Payment Demo</title>
</head>
<body>
<div>
<input type="text" id="uname " name="uname " value="">
<input type="text" id="pass" name="pass" value="">
</div>
</body>
</html>
This is java code.
WebView wb = (WebView) findViewById(R.id.webView1);
wb.loadUrl("file:///android_asset/web1.html");
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView web, String url) {
// TODO Auto-generated method stub
String uname = "email#mail.com";
String pass = "******";
web.loadUrl("javascript:(function(){document.getElementById('uname').value = '"+uname+"';})()");
web.loadUrl("javascript:(function(){document.getElementById('pass').value = '"+pass+"';})()");
}
});
Actually I do not understand why the file size of record.html will affect maintainence of the code. Read the html string (using Java reader class or what ever) from the html file in asset, use replaceAll function with Regex to replace all the [Custom] in the html file. How long the html is should not really affect how you maintain the code. It should rather be a performance problem, or the string is really really long that exceeds the java String limit.
some code I have used before :
InputStream is = getApplicationContext().getAssets().open("details/product_jsmodify.html");
Reader r = new InputStreamReader(is);
String details = Utils.readertoString(r);
details = details.replace("%product_name%",productName );
Utils is my class doing the conversion to string. I am not using Regex here as I am only replacing word for once. Then I load the string like Cata does. It is quite clean I suppose.
Yes you can do that by loading your page in a String and then load that string in your WebView.
Eg:
String summary = "<html><body>You scored <b>192</b> points.</body></html>";
webview.loadData(summary, "text/html", null);
Taken from here
This one worked for me, with the html along with the text and images.
InputStream is = getAssets().open(html_name);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
String str = new String(buffer);
str = str.replace("InitialTextToBeReplaced", "TextAfterReplacement");
//Now instead of webview.loadURL(""), I needed to do something like -
webView.loadDataWithBaseURL("file:///android_asset/", str, "text/html", "UTF-8",null);