-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/test/java/ch/digitalfondue/jfiveparse/example/Example.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package ch.digitalfondue.jfiveparse.example; | ||
|
||
import ch.digitalfondue.jfiveparse.Document; | ||
import ch.digitalfondue.jfiveparse.JFiveParse; | ||
import ch.digitalfondue.jfiveparse.Node; | ||
|
||
import java.io.StringReader; | ||
import java.util.List; | ||
|
||
public class Example { | ||
|
||
public static void main(String[] args) { | ||
// directly from String | ||
Document doc = JFiveParse.parse("<html><body>Hello world!</body></html>"); | ||
System.out.println(JFiveParse.serialize(doc)); | ||
|
||
// from reader | ||
Document doc2 = JFiveParse.parse(new StringReader("<html><body>Hello world!</body></html>")); | ||
System.out.println(JFiveParse.serialize(doc2)); | ||
|
||
// parse fragment | ||
List<Node> fragment = JFiveParse.parseFragment("<p><span>Hello world</span></p>"); | ||
System.out.println(JFiveParse.serialize(fragment.get(0))); | ||
|
||
// parse fragment from reader | ||
List<Node> fragment2 = JFiveParse.parseFragment(new StringReader("<p><span>Hello world</span></p>")); | ||
System.out.println(JFiveParse.serialize(fragment2.get(0))); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/test/java/ch/digitalfondue/jfiveparse/example/LoadHNTitle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package ch.digitalfondue.jfiveparse.example; | ||
|
||
import ch.digitalfondue.jfiveparse.Element; | ||
import ch.digitalfondue.jfiveparse.JFiveParse; | ||
import ch.digitalfondue.jfiveparse.NodeMatcher; | ||
import ch.digitalfondue.jfiveparse.Selector; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.Reader; | ||
import java.net.URL; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
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(); | ||
JFiveParse.parse(reader).getAllNodesMatching(matcher).stream() | ||
.map(Element.class::cast) | ||
.forEach(a -> System.out.println(a.getTextContent() + " [" + a.getAttribute("href") + "]")); | ||
} | ||
} | ||
} |