Ugh! JSoup meltdown - android

I'd like to preface my question with an apology - which will make this into a 2 part question....double apology.
I am struggling with JSoup (again) 1st apology for repeatedly asking and not learning well enough yet - so can anyone suggest some reading beyond the usual searches for something that will help me understand how to decipher the DOM each time I try this?
If you are still inclined to help, this time, within the doc returned I have:
<span id="priceProductQA1" class="productPrice">$29.99</span>
and I want to grab the href and price "29.99".
I've tried
doc = Jsoup.connect(srchStr).get();
for (Element choices : doc.select("a:has(.productPrice)")){
absHref = choices.attr("abs:href");
String pricetxt = choices.text();
and about 10 other ways to no avail. Any better ideas for me?

Here's another solution:
for( Element element : doc.select("span.productPrice") ) // Select all 'span' tags with a 'productPrice' class and iterate over them
{
final String price = element.text(); // save the price (= text of element)
final String link = element.parent().absUrl("href"); // Get the 'a' tag (= parent of 'span' tag) and its absolute (!) URL
// ...
}
Explanation:
Select the span tag because you can easy decide if its the one you need (has a class, while a has none)
Iterate over each element from 1.
Get the price of the element
Select the parent of the span tag since it contains the required url
Select the absolute URL; if you want the relative one, use attr("href") instead
Btw. if you are 100% shure there's only one of such elements on this website you can replace the for-Loop by Element element = doc.select("span.productPrice").first();, followed by the other two lines of code.

Related

Robotium - How can we find the exact index of an element in activity

I ask this question in case we are working in Black box testing.
I have researched about Robotium for few day. I learn by myself from Internet.
I also wrote some simple testcase and run it successfully. But when I search the index element (ex: an Edittext, I have to try index by index form 0 to x and get my expect index id).
Can we have another way to get form name, I have the ID name (txtEd1, btnLogin...), which we can access it form R class in White box testing, but i just ask about Black box in this case.
So can we have another way to get element by id or how can we get exact index number of an element in the activity.
I also used Hierarchy Viewer from DDMS and SDK tool to get index ID but it didn't work.
TextView tw = (TextView) solo.getText(<index>); << how can get exact index number?
you can try another way to loop on your views and find the right one.
First, you need to get all the views, and then loop on them like below:
views = solo.getCurrentViews();
for (int i = 0; i < views.size(); i++) {
if (views.get(i).getClass().getName().equals("android.widget.TextView")) {
tw = (TextView) views.get(i); //DO-YOUR-CHECK-BLABLABLA
}
}
I'm currently using atmosphere framework. With this framework you can select an element by: resource-id, index, content description, text, etc. by creating a selector. Also you can select an element by Css/xPath query. For example:
...
UiElementSelector selector = new UiElementSelector();
selector.addSelectionAttribute(CssAttribute.INDEX, "your index");
screen.waitForElementExists(selector, WAIT_FOR_ELEMENT_EXISTS_TIMEOUT);
UiElement targetElement = screen.getElements(selector).get(0);
...
You may find more good examples here.

JSoup Android: Get each results in a separate String

OK, What I want to achieve is to write each result JSoup fetches me in a separate String. Is this somehow possible? I can get the first and last with a function but, yea, then the rest is lost.
right now i have this in my doInBackground:
// Connect to the web site
Document document = Jsoup.connect(url).get();
// Using Elements to get the Meta data
Elements titleElement = document.select("h2[property=schema:name]");
// Locate the content attribute
date1 = titleElement.toString();
Log.e("Date", String.valueOf(Html.fromHtml(date1)));
With this i get a list of results which is nice, but i'd like to have every result in a separate String.
Thanks in advance, if you need anything more please ask :)
I read through the documentation carefully again and found this:
element.eq(n).text
The "n" defines which position to get, the .text strips all the html and makes it a readable text

output text line by line

I write app for Android such gets data from server in JSON format. Now I get this value in string, but in my application it must look like:
Route:
1)first point
2)secon point
3).....
n) n point
I read that in Android in textView I can do it if string will be with html tags but I think it is not the best variant. After Android I must do it in iPhone now I don't know how to do that there. Send Routes as Array is not good variant too. Can you say what is the best way to decide this problem?
Have a look here you will have to find the good pattern .
Hence you have separated strings just use a list View with an ArrayAdapter.
I am not so good with regex but i think it should like : [1-9][0-9]) [[a-f][0-9]]+
I couldn't comment b/c of rep, sorry. Could you provide an example of returned JSON string. I think JSON format can be parsed with ease.
If this the case you can parse it in a loop (or another way. I'm not that good at it)
String[] parseIt (String JSON){
String[] list=JSON.split("\\d\\)");
String[] rlist=new String[list.length-1];
for(int i=0;i<list.length-1;i++){
rlist[i]=list[i+1].trim();
}
return rlist;
}
This might do trick. But you should edit result. I didn't test yet
Edit: I edited code. It simply return the address now with leading whitespace. You can get rid off them using. String trim() method like;
list[1].trim();
Do it in loop and don't care about first element (index 0).
Edit 2: Now it should work

Android find Text Group and Highlight in ListView

I am performing a search over Strings in an ArrayList. If the Term is found I need to highlight that term and return the a string with the word where the term is present plus a word before and after that word!
Example:
Term = “some”
Searched String = “This is my awesome test String!”
Result = “my awesome test” (“some” should be highlighted here)
First off I am useless with RegEx and wouldn’t know where to start and secondly I’m not sure how to highlight text in a ListView, there are 3 TextViews per Row and I pass an Array with Data objects to the Adapter. Can I just give the Data Object Spanndable’s for highlighting?!
After some trial and error and some help i got this which seems to do the trick
(^|\S*\s)\S*term\S*($|\s\S*)

Android: How to parse a HTML site and take only some specific data?

I have a question:
I have a link: http://wap.nastabuss.se/its4wap/QueryForm.aspx?hpl=Teleborg+C+(V%C3%A4xj%C3%B6)
and I wanna take only some specific data from this link and to show in textview in Android.
Is this possible in Android, I mean is there any chance by parsing or I don't know, you can suggest me guys.
For example I just want to take this column Nästa tur (min) from that site.
Regards
JSoup is pretty nice and getting popular. Here's how you could just parse the whole table:
URL url = new URL("http://www.nseindia.com/content/equities/niftysparks.htm");
Document doc = Jsoup.parse(url, 3000);
Element table = doc.select("table[title=Avgångar:]").first();
Iterator<Element> it = table.select("td").iterator();
//we know the third td element is where we wanna start so we call .next twice
it.next();
it.next();
while(it.hasNext()){
// do what ever you want with the td element here
//iterate three times to get to the next td you want. checking after the first
// one to make sure
// we're not at the end of the table.
it.next();
if(!it.hasNext()){
break;
}
it.next();
it.next();
}
If the parsing seems simple enough and you want you could also use regular expressions to find the correct part of the html. Regular expressions will be useful to know at some point anyway. Using some XML/HTML parsing library is the more flexible way to do it (XMLReader for example).

Categories

Resources