Asomuth
Asomuth

Reputation: 25

Jsoup casting Element as TextNode causes exception

what am trying to parse using jsoup is the following Numéro d'arrêt : 5216 and Numéro NOR : 63066, but nothing seems to works any advice and suggestions will be greatly appreciated:

`

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

`

UPDATE :

i got this code to work but it keep giving me this exception org.jsoup.nodes.Element cannot be cast to org.jsoup.nodes.TextNode :

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");
                }

UPDATE :

here is what am trying to parse image link is below.

Parsing text outside html tags

Upvotes: 1

Views: 1077

Answers (2)

Eritrean
Eritrean

Reputation: 16498

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);
    } 
}

Upvotes: 1

Eritrean
Eritrean

Reputation: 16498

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());
 }

Upvotes: 1

Related Questions