[英]Extract all HTML tags, including closing tags, from file using Java without using external library like Jsoup
我有此代碼,它將接收一個HTML文件,獲取所有打開的HTML標記,然后打印它們。 我想知道是否有辦法在此代碼中也包含結束標記。 所以現在它打印:
<html>
<head>
<title>
<body>
<table>
<p>
<a>
<p>
etc. etc.
我也在尋找要與結束標簽一起打印的標簽。
<p>
<a>
</a>
</p>
到目前為止,這是我的代碼:
try {
BufferedReader in = new BufferedReader(new FileReader("test.html"));
String line;
StringBuilder stringBuilder = new StringBuilder();
while ((line = in.readLine()) != null) {
stringBuilder.append(line);
}
String pageContent = stringBuilder.toString();
Pattern pattern = Pattern.compile("<(?!!)(?!/)\\s*([a-zA-Z0-9]+)(.*?)>");
Matcher matcher = pattern.matcher(pageContent);
while (matcher.find()) {
String tagName = matcher.group(1);
System.out.println("<" + tagName + ">");
}
in.close();
}
編輯:有沒有一種方法,而無需使用Jsoup這樣的外部庫? 編輯2:我將Pattern.compile更改為this-> <([[a-zA-Z0-9] + | / [a-zA-Z0-9] +)(。*?)>,它可以正常工作。 謝謝。
如果可以使用外部庫,可以按照此處所述使用JSoup。 使用Jsoup從html文件中提取標簽
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.