[英]Extraction of HTML Tags using Java
I wanted to extract the various HTML tags available from the source code of a web page is there any method in Java to do that or do HTML parser support this? 我想从网页的源代码中提取各种HTML标签,有没有Java中的方法可以做到这一点,还是HTML解析器支持这个?
I want to seperate all the HTML tags . 我想分开所有的HTML标签。
Java comes with an XML parser with similar methods to the DOM in JavaScript: Java附带了一个XML解析器,其中包含与JavaScript中的DOM类似的方法:
DocumentBuilder builder = DocumentBuilderFactory.newDocumentBuilder();
Document doc = builder.parse(html);
doc.getElementById("someId");
doc.getElementsByTagName("div");
doc.getChildNodes();
The document builder can take many different inputs (input stream, raw html string, etc). 文档构建器可以采用许多不同的输入(输入流,原始html字符串等)。
http://download.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Document.html http://download.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/Document.html
The cyber neko parser is also good if you need more. 如果您需要更多,网络neko解析器也很好。
You can use regular expressions. 您可以使用正则表达式。 If your html is valid XML -- you can use XML parser
如果你的html是有效的XML - 你可以使用XML解析器
You can write your own util
method to extract tags. 您可以编写自己的
util
方法来提取标记。
Check for <
and />
or >
for complete tag and write those tags to another file. 检查
<
和/>
或>
是否有完整标记,并将这些标记写入另一个文件。
I've used HTMLParser in one project, was pretty happy with it. 我在一个项目中使用过HTMLParser ,非常满意。
Edit: If you check the samples page, the parser sample does pretty much what you're asking for. 编辑:如果您检查示例页面, 解析器示例几乎可以满足您的要求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.