有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Jsoup将元素转换为TextNode会导致异常

我试图使用jsoup解析的是以下Numéro d'arrêt:5216和Numéro NOR:63066,但似乎没有任何效果。我们将非常感谢您的任何建议:

`

<div class="1st">
<p>
<h3>Numérotation : </h3>
Numéro d'arrêt : 5216
<br />
Numéro NOR : 63066
</div>

`

更新:

我得到了这个代码,但它一直给我这个例外组织。jsoup。节点。元素不能强制转换为组织。jsoup。节点。文本节点:

Document tunisie = Jsoup.connect("http://www.juricaf.org/arret/TUNISIE-COURDECASSATION-20060126-5216").get();

for (Element titres : tunisie.select("div.arret")){

                    String titre = titres.select("h1#titre").first().text();
                    System.out.println(titre);
                    System.out.println("\n");
                }


                for (Element node : tunisie.select("h3")) {
                    TextNode numérodarrêt = (TextNode) node.nextSibling();
                    System.out.println(" " + numérodarrêt.text());
                    System.out.println("\n");

                }

                //NuméroNOR et Identifiant URN LEX
                for (Element element2 : tunisie.select("br")) {
                    TextNode NuméroNOR_IdentifiantURNLEX = (TextNode) element2.nextSibling();
                    System.out.println(" " + NuméroNOR_IdentifiantURNLEX.text());
                    System.out.println("\n");
                }

更新:

下面是我试图解析的图片链接

Parsing text outside html tags


共 (2) 个答案

  1. # 1 楼答案

    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    
    public class JsoupTest {
    
        public static void main(String[] args) throws IOException {
           Document tunisie = Jsoup.connect("http://www.juricaf.org/arret/TUNISIE-COURDECASSATION-20060126-5216").get();
           // get the first div in class arret
           Element arret = tunisie.select("div.arret").first();   
           // select h1 tag by its ID to get the title 
           String  titre = arret.select("#titre").text();
           System.out.println(titre);
           // to get the text after h3 select h3 and go to next sibling
           String txtAfterFirstH3    = arret.select("h3").first().nextSibling().toString();
           System.out.println(txtAfterFirstH3);
           // select first br by its index; note first br has the index 0; and call nextSibling to get the text after the br tag
           String txtAfterFirstBr    = arret.getElementsByTag("br").get(0).nextSibling().toString();
           System.out.println(txtAfterFirstBr);
           // the same as above only with next index
           String txtAfterSecondBr    = arret.getElementsByTag("br").get(1).nextSibling().toString();
           System.out.println(txtAfterSecondBr);
        } 
    }
    
  2. # 2 楼答案

    String html = "<div class=\"1st\">\n" +
                            "<p>\n" +
                            "<h3>Numérotation : </h3>\n" +
                            "Numéro d'arrêt : 5216\n" +
                            "<br />\n" +
                            "Numéro NOR : 63066\n" +
                        "</div>";
    
     Document doc = Jsoup.parse(html);
     Elements divs = doc.select("div.1st");
     for(Element e : divs){
          System.out.println(e.ownText());
     }