我有这个用于读取 XML 文件的代码:
try {
File fXmlFile = new File(fullFilePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("task");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
int i = temp;
MyTasks.customers[i][11] = getTagValue("att1", eElement);
MyTasks.customers[i][12] = getTagValue("att8", eElement);
MyTasks.customers[i][13] = getTagValue("att2", eElement);
MyTasks.customers[i][14] = getTagValue("att7", eElement);
MyTasks.customers[i][15] = getTagValue("att3", eElement);
MyTasks.customers[i][16] = getTagValue("att4", eElement);
MyTasks.customers[i][18] = getTagValue("att5", eElement);
MyTasks.customers[i][19] = getTagValue("att6", eElement);
//ect
}
}
} catch (Exception e) {
CreateLog.addToLog("[ReadXML]" + e.toString());
}
发生错误后是否可以继续读取 XML 文件。如果一个属性不存在,则整个脚本将停止读取并捕获错误。
提前致谢