HTML4J 介绍
HTML4J 是一个 Java 解析 HTML 的类库。示例代码:
Reader re = ... // Create the document HTMLDoc doc = new HTMLDoc(); // Load its content doc.load(re); // Get the HTML HTMLFragment html = doc.getHTML(); // Create a 'date' Meta-tag HTMLTag tag = HTMLTag.parse("<Meta name=\"date\" content=21/01/2001>"); // Insert it just before the title html.insertBefore(html.findTagByName("title"), tag); // Create a paragraph tag = HTMLTag.create("p"); // Insert '<p>Paragraph</p>' just before a tag with id="someid" html.insertBefore(html.getIdFinder("someid").getTag().getPosition(), tag.toString("Paragraph")); // Create an anchor to foo.html HTMLTag anchor = HTMLTag.parse("<a href=\"foo.html\">"); // We Could also do a 'HTMLTag.create("a")' and then set the 'href' // attribute using getAttributes().setAttribute("href", "foo.html") // // Now we get a tag block with id="otherid" tag = html.getIdFinder("otherid").getTagBlock(); // Replace the tag that has id="otherid" by the same tag // embraced by the foo.html anchor html.replace(tag.getBlockPosition(), anchor.toString(tag)); // For example, if the 'otherid' tag was 'img src="something.jpg"', // then the result would be: // '<a href="foo.html"><img id="otherid" src="something.jpg"></a>' // tag = html.getTagByName("Meta"); // We just got the first 'Meta' tag found in the document, and Now we // set its name attribute to 'last_update', and its value // (the 'content' attribute) to "20/01/2001" tag.getAttributes().setAttribute("name", "last_update"); tag.getAttributes().setAttribute("content", "20/01/2001"); // Commit the changes to the 'Meta' tag to the document html.update(tag);
HTML4J 官网
http://informatica.info/projects/html4j/index.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。