简体   繁体   English

Java IndexOf找不到正确的数据

[英]Java IndexOf not finding right data

I have a java application that needs to parse HTML elements from an HTML page. 我有一个Java应用程序,需要解析HTML页面中的HTML元素。 My simple HTML test is setup as such: 我的简单HTML测试是这样设置的:

<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
  div {width:100%;height:100px;background-color:blue;}
</style>
</head>
<body>
  <div></div>
</body>
</html>

My code will be setup such that it will search the document for this string: "<style" 我的代码将被设置为将在文档中搜索以下字符串:“ <style”

And then search for the closing carot: ">" because the user may have typed any of these combinations for their HTML file: 然后搜索结束符:“>”,因为用户可能已经为其HTML文件输入了以下任意组合:

<style type="text/css">

or

<style type = "text/css" >

or

<style type = 'text/css' >

or 

<style type='text/css'>

etc..

So my method is to find the "style" tag and everything up to its closing carot 因此,我的方法是找到“样式”标签及其所有内容

Then find the closing style tag: 然后找到结束样式标签:

</style>

Then grab everything between those two entities. 然后抓住这两个实体之间的所有东西。

Here's my files with their code: 这是我的文件及其代码:

************strings.xml************

String txt_style_opentag = "<style"
String txt_end_carrot = ">"
String txt_style_closetag = "</style>"

***********************************





************Parser.java************
public static String getStyle(Context context, String text) {
    String style = "";

    String openTag = context.getString(R.string.txt_style_opentag);
    String closeTag = context.getString(R.string.txt_style_closetag);
    String endCarrot = context.getString(R.string.txt_end_carrot);

    int openPos1 = text.indexOf(openTag);
    int openPos = text.indexOf(endCarrot, openPos1);
    int closePos = text.indexOf(closeTag, openPos1);

    if (openPos != -1 && closePos != -1)
        style = text.substring(openPos + openTag.length(), closePos).trim();

    if (style != null && style.length() > 0 && style.charAt(0) == '\n')     // first \n remove
        style = style.substring(1, style.length());

    if (style != null && style.length() > 0 && style.charAt(style.length() - 1) == '\n')    // last \n remove
        style = style.substring(0, style.length() - 1);

    return style;
}
********************************************************

My result is close, but not right. 我的结果接近,但不正确。 The result is this: 结果是这样的:

{width:100%;height:100px;background-color:blue;}

If you notice, it is missing the "div" part. 如果您注意到,它缺少“ div”部分。 It should look like this: 它看起来应该像这样:

div {width:100%;height:100px;background-color:blue;}

What am I doing wrong here. 我在这里做错了。 Can anyone help? 有人可以帮忙吗?

You're taking the substring from the end of your opening tag (the closing bracket > ) and adding the length of the opening tag (rather than endCarrot ), thus moving the start of the substring ahead of where you want it to be. 您将从开始标签的末尾(闭合括号> )中获取子字符串,并添加开始标签的长度(而不是endCarrot ),从而将子字符串的开头移至想要的位置。 You want to do 你想做

style = text.substring(openPos + endCarrot.length(), closePos).trim();

Of course...right after I ask for help I finally figure it out. 当然...在我寻求帮助后,我终于弄明白了。 The following code should be changed 以下代码应更改

FROM: 从:

style = text.substring(openPos + openTag.length(), closePos).trim();

TO: 至:

style = text.substring(openPos + endCarrot.length(), closePos).trim();

Sorry for the post. 抱歉,该职位。 And thanks for the recommendations 并感谢您的建议

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

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