使用DocumentBuilderFactory来解析xml文件
首先是主函数代码:
public static void main(String[] args) {
try {
final Document xml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(Main.class.getResourceAsStream("/actions.xml"));
Reader reader = new Reader();
reader.load(new Entry(xml.getDocumentElement()));
} catch (final IOException e) {
} catch (final SAXException e) {
}catch (final ParserConfigurationException e) {
}
}
这里使用了DocumentBuilderFactory::newInstance()获取了一个DocumentBuilderFactory实例。
然后调用DocumentBuilderFactory::newDocumentBuilder()官方文档的描述如下:
Creates a new instance of a DocumentBuilder using the currently configured parameters.
然后使用DocumentBuilder::parse(String uri) parse一个xml文件,结果是返回一个Document实例,同时这个方法需要捕捉IO和SAX异常,
其中的SAXException时parse发生错误时会抛出的错误。