当我运行下面的代码时,我收到:
[Fatal Error] :1:1: Content is not allowed in prolog.
org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.
我知道字符串html
不允许内容,但我想禁止所有错误。
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import org.w3c.dom.*;
import org.xml.sax.InputSource;
import javax.xml.xpath.*;
import javax.xml.parsers.*;
public class Test {
public static void main(String[] args){
String html="---<html><div id='teste'>Teste</div><div id='ola'>Ola tudo ebm!</div></html>";
try{
XPath xpath = XPathFactory.newInstance().newXPath();
String xpathExpression = "//div[@id='ola']";
InputStream is = new ByteArrayInputStream(html.getBytes());
InputSource inputSource = new InputSource(is);
NodeList nodes = (NodeList) xpath.evaluate
(xpathExpression, inputSource, XPathConstants.NODESET);
int j = nodes.getLength();
for (int i = 0; i < j; i++) {
System.out.println(nodes.item(i).getTextContent());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}