An open API service indexing awesome lists of open source software.

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

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/)