gpt4 book ai didi

java - 一个属性不存在后继续阅读 XML

转载 作者:行者123 更新时间:2023-11-29 08:02:48 25 4
gpt4 key购买 nike

我有读取 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文件。如果一个属性不存在,整个脚本将停止读取并捕获错误。

提前致谢

最佳答案

这是可能的。在 for-loop 中添加一个 try-catch 语句,如果捕获异常,只需使用 continue; 语句。

关于java - 一个属性不存在后继续阅读 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13375247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com