Java 在一个属性不存在后继续读取XML

Java 在一个属性不存在后继续读取XML,java,android,xml,try-catch,Java,Android,Xml,Try Catch,我有以下读取XML文件的代码: try { File fXmlFile = new File(fullFilePath); DocumentBuilderFactory dbFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder()

我有以下读取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循环中添加try-catch语句,如果捕获到异常,只需使用continue;声明。

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

将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){...}
        }
将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){...}
        }
像这样的

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.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;
}

您将不会收到其余的数据

getTagValue方法是什么?也许您应该使用一个方法来重载它,该方法接受一个额外的默认值,而不是在缺少属性时返回该值而失败?也许您应该使用一个方法来重载它,该方法接受一个额外的默认值,而不是在缺少属性时返回该值。