0
我有一個文本,我想提取一些字符串立足於正則表達式模式:速度:找到mathing字符串,並將其存儲在一個數組
<div>This is a text</div><div> </div><div>here is another text</div>
如何我所有的OCCURENCES存儲<div>
和</div>
之間在數組中避免空/空字符串?
謝謝。
我有一個文本,我想提取一些字符串立足於正則表達式模式:速度:找到mathing字符串,並將其存儲在一個數組
<div>This is a text</div><div> </div><div>here is another text</div>
如何我所有的OCCURENCES存儲<div>
和</div>
之間在數組中避免空/空字符串?
謝謝。
看來你想解析來自html/xml文檔的內容。 Velocity對於獲取一串字符串並將它們放在div標籤中是有用的......而不是相反。
HtmlCleaner是一種將html格式化爲xml的有用工具(即包含關閉p標籤和東西)。然後,您可以使用xpath輕鬆獲取div標籤的內容。
下面是一些未經測試的代碼,應該讓你開始:
try {
HtmlCleaner cleaner = new HtmlCleaner();
TagNode node = cleaner.clean(htmlString);
Object[] elements = node.evaluateXPath("//div");
for(Object element : elements){
System.out.println(((TagNode) element).getText().toString());
}
} catch (IOException e) {
Logger.getLogger().error(ExceptionUtils.getStackTrace(e));
} catch (XPatherException e) {
Logger.getLogger().error(ExceptionUtils.getStackTrace(e));
}