当前位置: 软件>java软件
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 = HTMLT...
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("");
// Insert it just before the title
html.insertBefore(html.findTagByName("title"), tag);
// Create a paragraph
tag = HTMLTag.create("p");
// Insert 'Paragraph
' 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("");
// 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="/tech-soft-java/something.jpg"',
// then the result would be:
// '
'
//
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);
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。