gpt4 book ai didi

java - 如何将带有 HTML 标签的字符串放入数组或列表中?

转载 作者:行者123 更新时间:2023-11-30 01:51:20 24 4
gpt4 key购买 nike

我有一个字符串,其中包含一些 html 标签,并且一个字符串中有多个数据。我需要检查 UI 上的元素是否存在于该字符串中。我能够删除标签,但我不确定如何将字符串转换为数组或列表,以便更容易比较。

例如,来自数据库的字符串是:

<dl style="float: left; text-align: left; width: 50%;">
<dt>
Note1amp;M
</dt>
<dd>
- This is an example
</dd>
<dt>
Note2
</dt>
<dd>
- Example 2
</dd>
<dt>
Note 3
</dt>
<dd>
- This is example 3
</dd>

用户界面中的文本是

Note1 - This is an example

其中 Note1 是一个元素

这是另一个元素的示例

到目前为止,我必须删除标签并尝试放入列表中

public String[] verifyData(Strint txtFromDB) {
String[] txt = new String[3];
boolean compareValue1 = false, compareValue2 = false;
boolean boolBack = false;
WebElement abbreviation = driver.findElement(By.xpath(itemLocatorP1));
WebElement fullName = driver.findElement(By.xpath(itemLocatorP2));
String p1, p2;

if((abbreviation.isDisplayed()) && (fullName.isDisplayed())) {
try {
getMenu().scroll_To_View_Web_Element(itemLocatorP1);
p1 = getUITxt(itemLocatorP1); // getting a text from the UI;
getMenu().scroll_To_View_Web_Element(itemLocatorP2);
p2 = getUITxt(itemLocatorP2); // getting the second part text from the UI:
txt[0] = p1; // Note 1
txt[1] = p2; // - This is an example

System.out.println("Array txt -> " + txt[0]);
}
catch(Exception e) {
txt[0] = "Blank";
System.out.println("Array txt Exception-> " + txt[0]);
}
// removing some html txt from the txtFromDB so that it can match with the UI
txtFromDB = txtFromDB.replaceAll("<dt>", "");
txtFromDB = txtFromDB.replaceAll("</dt>", "");
txtFromDB = txtFromDB.replaceAll("<dd>", "");
txtFromDB = txtFromDB.replaceAll("</dd>", "");
txtFromDB = txtFromDB.replaceAll(">", "");
txtFromDB = txtFromDB.replaceAll("</dl>", "");
txtFromDB = txtFromDB.replaceAll("</dl", "");
txtFromDB = txtFromDB.replaceAll("<dl style=", "");
txtFromDB = txtFromDB.replaceAll("float: left; text-align: left; width: 50%;", "");
txtFromDB = txtFromDB.replaceAll("\"\"", "");
txtFromDB = txtFromDB.replaceAll("&nbsp;", " ");
txtFromDB = txtFromDB.replaceAll("amp;", "");
txtFromDB = txtFromDB.replaceAll("&nbsp;", " ");
txtFromDB = txtFromDB.replaceAll("&rsquo;s", "’s");
txtFromDB = txtFromDB.replaceAll("&ndash;", "–");
txtFromDB = txtFromDB.replaceAll("(?m)^[ \t]*\r?\n", "");
System.out.println("DB Txt -> " + txtFromDB);

String[] temp = txtFromDB.split("\\n");

for(String x : temp) {
System.out.println(x);
if((x.contains(txt[0])) && (x.contains(txt[1]))) {
System.out.println(x + " from DB matches the UI -> " + txt[0] + txt[1]);
compareValue1 = true;
break;
}
else {
System.out.println("Still Searching.....");
}
}

if(compareValue1 )
boolBack = true;
else
boolBack = false;
}
else {
System.out.println("No such element was found in the page");
txt[0] = "Blank";
boolBack = false;
}
txt[2] = Boolean.toString(boolBack);
return txt;
}

所以我想做的是输入 <dt>Note1</dt> and <dd>-This is an example</dd>作为一个字符串,例如:Note 1 - This is an example在列表或数组中,以便我可以与 UI 上的任何数据进行比较。

最佳答案

使用 JSoup

您可以考虑使用 JSoup,而不是自己解析它。

https://en.wikipedia.org/wiki/Jsoup

使用 JSoup,您可以删除所有 html 并通过以下方式获取文本:

String html = "<p>example</p>";
Document doc = Jsoup.parse(html);
System.out.println(doc.text()); // doc.text() returns the text only from the html

这将输出:

example

使用 JSoup,您还可以找到具有特定 id 的元素,以便更轻松地将它们分开。

String html = "<dt>example</dt>";
Document doc = Jsoup.parse(html);
Elements dts = doc.getElementsByClass("dt");

关于java - 如何将带有 HTML 标签的字符串放入数组或列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55942778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com