简体   繁体   English

如何使用Jsoup在Android中获得Element的一部分

[英]How to get part of an Element in Android using Jsoup

I'm trying to find a way to get the last part of a Element and set it to a textView Here is my code for getting the element and setting it to a textView. 我正在尝试找到一种方法来获取元素的最后一部分并将其设置为textView。这是获取元素并将其设置为textView的代码。

Element burnabyStatus = doc.getElementsByClass("main-campus-status").first();
String b_status = burnabyStatus.text();
TextView tv_b_status = (TextView) findViewById(R.id.b_status);
tv_b_status.setTypeface(tf, Typeface.BOLD);
tv_b_status.setText(b_status);

As of now burnabyStatus.text() is equal to "Burnaby Campus Open" but I want my textView to just show "Open" or "Closed" for when it changes on the website. 截至目前,burnabyStatus.text()等于“ Burnaby Campus Open”,但我希望我的textView在网站上更改时仅显示“ Open”或“ Closed”。 Here is the website from where I am getting the information from here 这是我从这里获取信息的网站

After looking at the website, The word you need "Open" is in a "h1" child element under the "main-campus-status" div element, hence what you need to do is get the text of the "h1" element, and you can do it like this: 浏览完网站后,您需要在“ main-campus-status” div元素下的“ h1”子元素中添加“ Open”一词,因此您需要获取“ h1”元素的文本,并且您可以这样做:

Element burnabyStatus = doc.getElementsByClass("main-campus-status").first();
Elements h1 = burnabyStatus.select("h1");
burnabyStatus = h1.get(0);
String b_status = burnabyStatus.text();
TextView tv_b_status = (TextView) findViewById(R.id.b_status);
tv_b_status.setTypeface(tf, Typeface.BOLD);
tv_b_status.setText(b_status);

The lines I've added are: 我添加的行是:

Elements h1 = burnabyStatus.select("h1");
burnabyStatus = h1.get(0);

Which selects all h1 elements under the "main-campus-status" div which when you call "text()" will only get you "Open" or "Closed". 它选择“ main-campus-status” div下的所有h1元素,当您调用“ text()”时,只会使您“打开”或“关闭”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM