[英]jsoup parsing string value in android
I am learning jsoup. 我正在学习jsoup。 I want to parse the below script :
我想解析以下脚本:
<script>
_cUq="1lj9lodlnq";
</script>
After parsing output : 1lj9lodlnq
Here is what I am trying: 解析
output : 1lj9lodlnq
这是我正在尝试的:
String str = element.ownText().toString();
str = str.replace("\r","");
str = str.replace("\n","");
str = str.replace("<script>","");
str = str.replace("</script>","");
System.out.println(str);
if(str.contains("="))
split = str.split("=");
On debugging I can see the script is stored in the element tag but on assigning to str I get "". 在调试时,我可以看到脚本存储在element标记中,但是在分配给str时得到“”。 Correct me where I am going wrong.
纠正我哪里出问题了。
You can extract the inner Javascript with Jsoup. 您可以使用Jsoup提取内部Javascript。 This has the plus that your code is much easier to maintain.
这样做的好处是您的代码易于维护。 Also, you can use regular expressions to rule out the whitespaces instead of String.replace() them one by one.
另外,您可以使用正则表达式排除空格,而不是String.replace()一对一地排除空格。
import org.jsoup.Jsoup;
import org.junit.Test;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
public class JSoupSO {
@Test
public void script() {
String s = "<script>\n" +
"_cUq=\"1lj9lodlnq\";\n" +
"</script>";
// let Jsoup parse the HTML
String innerJavascript = Jsoup.parse(s).data();
// remove all whitespaces
innerJavascript = innerJavascript.replaceAll("\\s", "");
assertThat(innerJavascript, is("_cUq=\"1lj9lodlnq\";"));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.