https://github.com/peteroupc/htmlparser
HTML5 Parser for Java
https://github.com/peteroupc/htmlparser
Last synced: 5 months ago
JSON representation
HTML5 Parser for Java
- Host: GitHub
- URL: https://github.com/peteroupc/htmlparser
- Owner: peteroupc
- License: other
- Created: 2013-03-02T16:12:27.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2025-03-25T04:04:41.000Z (over 1 year ago)
- Last Synced: 2025-04-19T19:26:14.092Z (over 1 year ago)
- Language: Java
- Size: 1.53 MB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
HTML5 parser for Java.
Takes an input stream or a file and returns an HTML document tree.
The API is currently only a subset of the DOM. Example:
IDocument doc=HtmlDocument.parseFile(filename);
for(IElement element : doc.getElementsByTagName("img")){
System.out.println(element.getAttribute("src"));
}
And here is a more complex example that gets all Open Graph and "image_src"
images specified on a Web page.
public static List getWebpageImages(String url) throws IOException {
IDocument doc;
List images=new ArrayList();
doc=HtmlDocument.parseURL(url);
for(IElement element : doc.getElementsByTagName("meta")){
if("og:image".equals(element.getAttribute("property")) ||
"og:image:secure_url".equals(element.getAttribute("property"))){
String content=HtmlDocument.getHref(element,element.getAttribute("content"));
images.add(content);
}
}
if(images.size()>0)return images;
for(IElement element : doc.getElementsByTagName("link")){
if("image_src".equals(element.getAttribute("rel"))){
String content=HtmlDocument.getHref(element);
images.add(content);
}
}
return images;
}
Copyright (C) 2013 Peter Occil. Licensed under the Expat License.
Sample code on this README file is dedicated to the public domain under CC0:
[https://creativecommons.org/publicdomain/zero/1.0/](https://creativecommons.org/publicdomain/zero/1.0/)