0

我遇到以下问题。我编写了一个读取已知结构的 html 文件的 java 片段(见下文)。我将其拆分为元素并使用 jericho 解析器获取它们的文本。然后,当我打印文本以确保检索正确时,我将每个元素添加到 ArrayList 中(显然检索是正确的)。您可以从 html 结构中猜到​​,如果 ArrayList 的元素 i 是 Email ,那么元素 i+1 就是 value。我认为这是一个范围问题,但无法弄清楚。你能帮我吗?我正在使用以下代码来获取值:

private static void setVariables() 
{
    count=0;
    for(String s:str)
    {

       if(s.contains("Email"))
       {
          email=getValue(email, count);

       }
     }      
}



private static String getValue(String var, int count) 
{
    var=str.get(count+1);
    count++;
    return var;

}




private static void getElementsText(Source source) 
{


    List<Element> elementListTd = source.getAllElements(HTMLElementName.TD);

    //Scroll through the list of elements "td" page
      int i=0;
    for (Element element : elementListTd) {
        if (element.getAttributes() != null) {
            String td = element.getAllElements().toString();
            String tag = "td";
         //   System.out.println("TD: " + td);
          //  System.out.println(element.getContent());
            String contentsAttributes = element.getTextExtractor().toString();
          //  System.out.println("line "+i+" "+contentsAttributes);
            if(!contentsAttributes.isEmpty())
            {   
            str.add(contentsAttributes);
            }


        }
    }

    str.add(" ");
    setVariables();
}



public static void main(String[] args) throws FileNotFoundException, IOException 
{

  FileReader fr=new FileReader("32EPS 2012-ABSTRACT ~ 1333134955.html");
  BufferedReader br=new BufferedReader(fr);

  getTextFromFile(br);
  Source source = new Source(html_txt);
  getElementsText(source);

}

HTML:

<tr><td class='text11' nowrap>Email</td><td class='text11'>peraueb@dummy.com<td></tr>
<tr><td class='text11' nowrap>Gender</td><td class='text11'>Mr<td></tr>
4

0 回答 0