Skip to content

Commit

Permalink
update example + add link for the entities.json
Browse files Browse the repository at this point in the history
  • Loading branch information
syjer committed Jan 14, 2025
1 parent 8bb88e0 commit 7426e06
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ See directory: https://github.com/digitalfondue/jfiveparse/tree/master/src/test/
### Fetch all titles+links on the front page of HN

```java
package ch.digitalfondue.jfiveparse.example;

import ch.digitalfondue.jfiveparse.Element;
import ch.digitalfondue.jfiveparse.JFiveParse;
import ch.digitalfondue.jfiveparse.NodeMatcher;
Expand All @@ -111,10 +113,13 @@ public class LoadHNTitle {
public static void main(String[] args) throws IOException {
try (Reader reader = new InputStreamReader(new URL("https://news.ycombinator.com/").openStream(), StandardCharsets.UTF_8)) {
// select td.title > span.titleline > a
NodeMatcher matcher = Selector.select().element("td").hasClass("title").withChild().element("span").hasClass("titleline").withChild().element("a").toMatcher();
NodeMatcher matcher = Selector.select().
element("td").hasClass("title")
.withChild().element("span").hasClass("titleline")
.withChild().element("a").toMatcher();
JFiveParse.parse(reader).getAllNodesMatching(matcher).stream()
.map(Element.class::cast)
.forEach(a -> System.out.println(a.getTextContent() + " [" + a.getAttribute("href") + "]"));
.forEach(a -> System.out.printf("%s [%s]\n", a.getTextContent(), a.getAttribute("href")));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
import com.google.gson.reflect.TypeToken;

/**
* Generate the file "entities-with-1-2-codepoint", used by Entities
* Generate the file "entities-with-1-2-codepoint", used by Entities.
* entities.json is from https://html.spec.whatwg.org/entities.json
*/
public class GenerateEntities {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ public class LoadHNTitle {

public static void main(String[] args) throws IOException {
try (Reader reader = new InputStreamReader(new URL("https://news.ycombinator.com/").openStream(), StandardCharsets.UTF_8)) {
// select td.title > a.storylink
NodeMatcher matcher = Selector.select().element("td").hasClass("title").withChild().element("a").hasClass("storylink").toMatcher();
// select td.title > span.titleline > a
NodeMatcher matcher = Selector.select().
element("td").hasClass("title")
.withChild().element("span").hasClass("titleline")
.withChild().element("a").toMatcher();
JFiveParse.parse(reader).getAllNodesMatching(matcher).stream()
.map(Element.class::cast)
.forEach(a -> System.out.println(a.getTextContent() + " [" + a.getAttribute("href") + "]"));
.forEach(a -> System.out.printf("%s [%s]\n", a.getTextContent(), a.getAttribute("href")));
}
}
}

0 comments on commit 7426e06

Please sign in to comment.