Search in sources :

Example 26 with Document

use of org.jsoup.nodes.Document in project jsoup by jhy.

the class ElementsTest method attr.

@Test
public void attr() {
    Document doc = Jsoup.parse("<p title=foo><p title=bar><p class=foo><p class=bar>");
    String classVal = doc.select("p").attr("class");
    assertEquals("foo", classVal);
}
Also used : Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 27 with Document

use of org.jsoup.nodes.Document in project jsoup by jhy.

the class ElementsTest method remove.

@Test
public void remove() {
    Document doc = Jsoup.parse("<div><p>Hello <b>there</b></p> jsoup <p>now!</p></div>");
    doc.outputSettings().prettyPrint(false);
    doc.select("p").remove();
    assertEquals("<div> jsoup </div>", doc.body().html());
}
Also used : Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 28 with Document

use of org.jsoup.nodes.Document in project jsoup by jhy.

the class ElementsTest method traverse.

@Test
public void traverse() {
    Document doc = Jsoup.parse("<div><p>Hello</p></div><div>There</div>");
    final StringBuilder accum = new StringBuilder();
    doc.select("div").traverse(new NodeVisitor() {

        public void head(Node node, int depth) {
            accum.append("<" + node.nodeName() + ">");
        }

        public void tail(Node node, int depth) {
            accum.append("</" + node.nodeName() + ">");
        }
    });
    assertEquals("<div><p><#text></#text></p></div><div><#text></#text></div>", accum.toString());
}
Also used : Node(org.jsoup.nodes.Node) Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 29 with Document

use of org.jsoup.nodes.Document in project jsoup by jhy.

the class ElementsTest method filter.

@Test
public void filter() {
    String h = "<p>Excl</p><div class=headline><p>Hello</p><p>There</p></div><div class=headline><h1>Headline</h1></div>";
    Document doc = Jsoup.parse(h);
    Elements els = doc.select(".headline").select("p");
    assertEquals(2, els.size());
    assertEquals("Hello", els.get(0).text());
    assertEquals("There", els.get(1).text());
}
Also used : Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Example 30 with Document

use of org.jsoup.nodes.Document in project jsoup by jhy.

the class ElementsTest method hasText.

@Test
public void hasText() {
    Document doc = Jsoup.parse("<div><p>Hello</p></div><div><p></p></div>");
    Elements divs = doc.select("div");
    assertTrue(divs.hasText());
    assertFalse(doc.select("div + div").hasText());
}
Also used : Document(org.jsoup.nodes.Document) Test(org.junit.Test)

Aggregations

Document (org.jsoup.nodes.Document)405 Test (org.junit.Test)194 Element (org.jsoup.nodes.Element)164 IOException (java.io.IOException)102 File (java.io.File)81 Elements (org.jsoup.select.Elements)78 ElementHandlerImpl (org.asqatasun.ruleimplementation.ElementHandlerImpl)51 ArrayList (java.util.ArrayList)41 Connection (org.jsoup.Connection)38 URL (java.net.URL)25 HashMap (java.util.HashMap)17 InputStream (java.io.InputStream)14 List (java.util.List)10 MalformedURLException (java.net.MalformedURLException)8 Logger (org.slf4j.Logger)8 Matcher (java.util.regex.Matcher)7 Jsoup (org.jsoup.Jsoup)7 LoggerFactory (org.slf4j.LoggerFactory)7 Pattern (java.util.regex.Pattern)6 HttpGet (org.apache.http.client.methods.HttpGet)6