Ask the help for regular expressions in Android - java

I am a beginner on developing Android's application. I have some problems about using regular expressions in Android.
In my application, I had write some code for get back the source code of a html page which I had turn it in String datatype.
And now I want to use regular expressions to retrieve some details in the html tags of this page. So, what should I do for it?
for example, in the html code,
<HTML>
.......
<TABLE class=tb_info cellSpacing=0 cellPadding=0 border=0>
<TR>
<TD class=pro_name vAlign=center colSpan=5>Abby Poon</TD>
</TR>
<TR>
<TD class=pro_name vAlign=center colSpan=5>Peter Pan</TD>
</TR>
</TABLE>
.........
</HTML>
And now I want to get back the value "Abby Poon" and "Peter Pan" which is in a table called "tb_info" and in a called "pro_name" for both two values.
Can anyone teach me how I can do it? Or you may give me some reference page for it with Android.
Thank you very much.

Do not parse HTML using regular expressions.
Instead, use an HTML Parser.

HTML is not a regular language, therefore you cannot use Regular Expressions against it 100% successfully. You need to use some sort of HTML parser instead.
You don't want to be driven insane.

Related

How to submit a form using Java HttpURLConnection when button is image

I am trying to write a program in Java using only the java.net.* libraries (i.e. HttpURLConnection, URL...) - this is a requirement for this project (i.e. no external packages like HttpClient, JSoup, etc...). The ultimate form submission 'Go button' is actually a picture that the user clicks. It's code snippet is here:
<script>
var form = $('pdb');
function proceed() {
form.submit();
}
</script>
<div id=input_nav>
<table class="nav_entry" onclick="proceed();" align=right>
<tr>
<td align="right">Next Step:<br/>Select Model/Chain</td>
<td align="right">Next Step:<br/>Select Model/Chain</td>
<td width="51" align="left"><img src="images/basic/next.png"></td>
</tr>
</table>
</div>
To my understanding, form submission using Java's .net library requires sending the HTML' code's gui object name-value pairs, but as can be seen from the HTML source code snippet, no such fields exist foe the button. Could someone please give me some direction with how this job can be accomplished?

Unable to set variables in a Thymeleaf template

I'm using Spring Boot with some templates to help generate some dynamic emails. Unfortunately, the templating engine isn't rendering my variables.
Backend Call
public String generateProblemOfTheDay(Model model) throws IOException {
Context ctx = new Context();
ctx.setVariable("potd", "Test Value");
//Process the template with the proper context variables
String html = templateEngine.process("index", ctx);
PrintWriter pWriter = new PrintWriter(Paths.PROBLEM_OF_THE_DAY_OUTPUT, "UTF-8");
pWriter.println(html);
pWriter.close();
log.info("done!");
log.info(html);
return html;
}
Segment of my template
.
.
<tr>
<td style="font-family:'Open Sans', Arial, sans-serif; font-size:15px; line-height:18px; color:#30373b;">
<br />
<div class="question-description">
[[${potd}]]
</div>
</td>
</tr>
.
.
I'm not sure why the template engine isn't processing the variables correctly. Is this the best way to add variables?
What I found does work
<label style="font-size: 12px;padding-bottom: 1em;" th:text="${potd}">Test</label>
Adding something like the following does indeed work.. I've seen many people use t he standard curly bracket notation without issues and wondering what is appropriate where.
Inlined expressions were changed from thymeleaf 2 to 3. I'm guessing you're using thymeleaf 2, which means you need the attribute th:inline="text" in order to get your expression to work.
<div class="question-description" th:inline="text">
[[${potd}]]
</div>
If you upgrade to thymeleaf 3, those expressions will work out of the box (and it even recommends you remove th:inline="text"). As for which way you should write expressions... it is pretty much opinion based. For the most part, I like using th:text directly in a tag. If you are appending a lot of strings together, you might use the other way. For example:
<span>Your answer was: [[${answer}]]</span>
is easier to read than
<span th:text="${'Your answer was:' + answer}"/>

Syntax error on selenium xpath expression

I'm building a selenium findElement by.xpath expression, very complex, and it appears there is a syntax error.
I don't really know XML language so I made mistake(s) obviously, could someone tell where are the mistake(s) ? :)
Thanks!
I have a list of projects on a web page, in each project there are sub-projects, I want a specific one, I know the name and the subName.
The html code is something like :
<table id="list_proj" class="table tablebas table-striped table-bordered">
<thead>
<tbody style="height:1em;overflow-y:scroll"></tbody>
<tbody style="height:1em;overflow-y:scroll"></tbody>
<tbody style="height:1em;overflow-y:scroll">
<tr class="caption">
<td class="app_name" style="background-color:#EFEFEF;" colspan="11">
<b>
Application SEVo
</b>
</td>
</tr>
<tr class="data"></tr>
<tr class="data"></tr>
<tr class="data">
<td title="Pas de commentaire !" style="text-align:left">
<img alt="MCO" src="/colibri/images/corner-dots.gif"></img>
MCO
</td>
<td>0.50</td>
<td></td>
<td colspan="2">
</tr>
<tr class="data">
</tbody>
<tbody style="height:1em;overflow-y:scroll"></tbody>
</table>
And my xpath expression :
driver.findElement(
By.xpath(
"((//a[contains(text(),'"+name+"')])[1]
.(ancestor::td[#class='caption'])[1]
.following-sibling::td[#class='data']
.descendant::(a[contains(text(),'"+subName+"')])[1])[1]"
));
Sorry for the mess ^^'
Use below xPath which is more better :-
//a[contains(.,'"+name+"')]/ancestor::tr/following::a[contains(.,'"+subName+"')]
for eg. :- //a[contains(.,'Application SEVo')]/ancestor::tr/following::a[contains(.,'MCO')]
Updated..
another one is :-
//a[contains(.,'"+name+"')]/following::a[contains(.,'"+subName+"')]
Hope it will help you..:)
I don't know xPath with selenium, but I think the syntax error should be in the line below
.descendant::(a[contains(text(),'"+subName+"')])[1])[1]
Should be
.descendant::(//a[contains(text(),'"+subName+"')])[1])[1]
? (note the "//a")
It should be something like:
//a[contains(text(),'"+name+"')]/ancestor::tr[#class='caption']/following-sibling::tr[#class='data']//a[contains(text(),'"+subName+"')]
Don't use index [1] everytime, If you are sure that only one element would be returned, there is no point of putting index in every descendant or ancestor call.
"((//a[contains(text(),'"+name+"')])[1]
.(ancestor::td[#class='caption'])[1]
.following-sibling::td[#class='data']
.descendant::(a[contains(text(),'"+subName+"')])[1])[1]"
There's so much wrong with this it's hard to know where to start. First, we don't know what's in the variables name and subName - if these strings contain single quotes then anything might happen (injection attack). Secondly, it seems to be using "." as a separator between steps in the path when it should use "/". Third, what follows an axis like "descendant::" must be a NodeTest, and that rules out a parenthesized expression.
I think there's a real problem in your approach. Writing a complex expression in a language you don't understand and then asking on SO what the syntax errors mean is not going to be a good way to make progress. (Once you've got rid of the syntax errors, will you then know what the XPath actually means?). Do some reading and learn the language properly; don't try to drive a car without taking driving lessons.

HTMLWorker.parseToList ignores background color

I'm using java i text for generating pdf. Some data in my pdf is from html. I'm using HTMLWorker.parseToList to create it.
My problem is that it ignores the background color that is written inside the html tags. Any idea why? How can i solve this?
Thanks in advance.
Spotty CSS Support strikes again.
iText doesn't currently support the backgroundcolor style. It does support the bgcolor attribute however, and attributes and styles are mapped into the same namespace.
So if you XSLT your incoming HTML, you could add a matching bgcolor attribute, or simply change the style string to bgcolor.
Ugly, but effective.
iText's HTML->PDF conversion code is supposed to get a major upgrade in the next release. The groundwork was laid in the 5.0.6 release, though I haven't seen any code changes that will actually improve the output as yet.
It works with bgcolor when used as a direct attribute (but not in style attribute with "background-color")
<table border="1" cellpadding="2">
<tr bgcolor="#C0C0C0">
<td><b>Gray Header</b></td>
<td><b>Second header</b></td>
</tr>
<tr>
<td style="color:green">Green text</td>
<td bgcolor="#FFC0C0">Red background</td>
</tr>
</table>
I tried the below workaround and it perfectly works for me.
.tr-bg{
background-color: #D3D3D3;
}
And use this class for required dom element.

Converting an ArrayList<someObjects> to an HTML table

I have a couple of ArrayLists with variable length and sometimes null. This ArrayList contains a bunch of objects.
The table should have columns based on (some) attributes of the object. And the table should be displayed on a jsp.
I have two ideas, one is to use a JSTL tag the other is to use JavaScript. And library suggestions are welcome.
JSTL is the standard, preferred way (unless you need to load it via ajax, for example)
<table>
<tr><td>Foo header</td><td>Bar header</td></tr>
<c:forEach items="${yourRequestScopedArrayList}" var="obj">
<tr>
<td>${obj.foo}</td>
<td>${obj.bar}</td>
</tr>
</c:forEach>
</table>
JSTL is better,
Javascript you should avoid as much as possible ,
I am not sure how you are going to render datatable using java script and Collection
How to use jstl with collection that has been demonstrated by Bozho in the same thread.
Javascript doesn't have access to the Java objects that live (I presume) on the server. The server code can make the ArrayLists available to the JSP which can then loop over them with a JSTL forEach tag.
How you make the ArrayLists "available" depends on the framework you're using, but the plain servlet way is just setting an attribute from the doPost method.
request.setAttribute("list1", arrayList1);
The loop would be something like
<table>
<tr><th>Column 1</th> <th>Column 2</th> <th>Column 3</th></tr>
<c:forEach var="row" items="${list1}">
<tr><td>${row.col1data}</td> <td>${row.col2data}</td> <td>${row.col3data}</td></tr>
</c:forEach>
</table>

Categories