【问题标题】:jSoup getting value of HTML tagjSoup获取HTML标签的值
【发布时间】:2016-01-10 00:39:19
【问题描述】:

我正在从互联网上读取一个 html 文件,当我读取该文件时,我的控制台的输出如下:

<string>
       <String1>
        text
       </String1>
       <level2>
        text2
       </level2>
       <level3>
        text3
       </level3>
       <level4>
        text4
       </level4>
       <level5>
         TEXT
       </level5>
</string>
<string>
           <String2>
            text
           </String2>
           <level2>
            text2
           </level2>
           <level3>
            text3
           </level3>
           <level4>
            text4
           </level4>
           <level5>
             THIS TEXT
           </level5>
    </string>

如何访问第二个字符串中的 level5 文本?我一直在尝试一整天都没有运气,并且非常感谢对此了解更多的人的一些意见。

这是我的代码:

String line = null;

            try {
                // FileReader reads text files in the default encoding.
                FileReader fileReader = new FileReader(String.valueOf(doc));

                // Always wrap FileReader in BufferedReader.
                BufferedReader bufferedReader = new BufferedReader(fileReader);

                while ((line = bufferedReader.readLine()) != null) {
                    Elements tdElements = doc.getElementsByTag("level1");
                    for(Element element : tdElements )
                    {
                        //Print the value of the element
                        System.out.println(element.text());
                    }

                }

                // Always close files.
                bufferedReader.close();
            } catch (FileNotFoundException ex) {
                System.out.println(
                        "Unable to open file '" +
                                doc + "'");
            } catch (IOException ex) {
                System.out.println(
                        "Error reading file '"
                                + doc + "'");
                // Or we could just do this:
                // ex.printStackTrace();
            }
        }
//
        catch (IOException e) {
            e.printStackTrace();
        }

【问题讨论】:

  • @JaredRummler 我将如何做到这一点,所以如果有两个选项,我会确保在选择 level5 之前满足条件(在 option2 标签而不是 option1 标签下)?我在上面更新了我的问题
  • @JaredRummler 真正的 HTML 看起来像示例。但是该代码导致应用程序崩溃..您可以再次检查 html 吗?我更新了。

标签: java html parsing jsoup


【解决方案1】:

下面的代码使用 JSoup 来解析您所引用的文本。变量“textToParse”是您提供的上述 html 代码。您可以使用 JSoup 的 Psuedo 选择器在 DOM 树中的特定位置查找元素。希望这就是您想要的。

Document document = Jsoup.parse(textToParse);
Elements stringTags = document.select("string:eq(1)");
for(Element e : stringTags) {
    System.out.println(e.select("level5").text());
}

//Output: THIS TEXT

【讨论】:

    【解决方案2】:

    您可以在此处使用 CSS 选择器:

    string:nth-of-type(2) > level5
    

    演示:http://try.jsoup.org/~8w_pfCxDhJwIseTKiKsQjQJOBRs

    说明

    string:nth-of-type(2) /* Select the 2nd string node in document... */
    > level5                /* ... then select all "level5" child nodes  */
    

    示例代码

    Document doc = ...
    Element level5Node = doc.select("string:nth-of-type(2) > level5").first();
    if (level5Node ==null) {
       throw new RuntimeException("Unable to locate level5 text...");
    }
    
    System.out.println(level5Node.text()); // THIS TEXT
    

    【讨论】:

      【解决方案3】:

      解决方案 1:你的 html 是有效的 XML:使用 XML 工具:

      您可以使用 XPath 获得您的第二级 5:“//string[2]/level5”

      方案二:用Jsoup解析得到文档 然后使用 Xpath 作为解决方案 1

      查看带有 XPath / XSoup 的 Jsoup:Does jsoup support xpath?

      解决方案 1:

      String xml="<root>"+your xml+"</root>";
      
      DocumentBuilderFactory builderFactory =DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = builderFactory.newDocumentBuilder();
      Document document = builder.parse(new InputSource(new StringReader(xml)));
      XPath xPath = XPathFactory.newInstance().newXPath();
      String expression="//string[2]/level5";
      String value = xPath.evaluate(expression, document);
      System.out.println("EVALUATE:"+value);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多