1

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

提前致谢

4

3 回答 3

3

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

于 2012-11-14T08:29:51.103 回答
1

放入try-catch循环内。

         for (int temp = 0; temp < nList.getLength(); temp++) {
           try{
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                Element eElement = (Element) nNode;
                ...
            }
          }catch(Exception ex){...}
        }
于 2012-11-14T08:30:29.643 回答
1

像这样的东西?

MyTasks.costumers[i][19] = callGetTagValue("x", eElement)


...

private callGetTagValue(String x, Object element){
try{
//foo
}catch(Exception e){
return "not found";
}
}

这样你就不会丢失可用的数据,当你调用 continue 时就是这种情况;

编辑:例如,您也可以这样:

try{
     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){
continue;
}

但是当异常发生在

MyTasks.customers[i][13] = getTagValue("att2", eElement);

您将不会收到其余数据

于 2012-11-14T08:31:38.330 回答