开始做的时候遇到有些文件没有title元素的,程序会报空指针,然后我就catch空指针异常,返回一个空字符串。可是遇到有些文件又会报异常报错,比如
[Fatal Error] index.jsp:1:2: The markup in the document preceding the root element must be well-formed.
Exception in thread "main" org.xml.sax.SAXParseException; systemId: file:/home/lilisong/NetBeansProjects/WebApplication1/web/index.jsp; lineNumber: 1; columnNumber: 2; The markup in the document preceding the root element must be well-formed.
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:253)
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:288)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)
at com.bjsxt.test.CreateHtml2_1.getTitle(CreateHtml2_1.java:50)
at com.bjsxt.test.CreateHtml2_1.listFile(CreateHtml2_1.java:28)
at com.bjsxt.test.CreateHtml2_1.listFile(CreateHtml2_1.java:39)
at com.bjsxt.test.CreateHtml2_1.test(CreateHtml2_1.java:72)
at com.bjsxt.test.CreateHtml2_1.main(CreateHtml2_1.java:82)
Java Result: 1
我的代码是
public static String getTitle(File f) throws Exception {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(f);
Node title_tag = document.getElementsByTagName("title").item(0);
return title_tag.getFirstChild().getTextContent();
} catch (NullPointerException e) {
return "";
}
}
请教的问题如标题,请各位大神给看看
对于你要从jsp界面里面的文字,要么是固定写死的, 那你的地图也可以写死呀;要么是从数据库里面或者其他文件里面找出来的, 那你做地图的时候为什么就不可以去数据库或者其他文件里面去找呢