Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用java异常进行条件检查_Java_Grails_Saxparser - Fatal编程技术网

使用java异常进行条件检查

使用java异常进行条件检查,java,grails,saxparser,Java,Grails,Saxparser,我正在grails应用程序中创建一个xml文件上传程序。有两种类型的文件,Ap和ApWithVendor。我希望使用SAXParser自动检测文件类型并将xml转换为正确的对象 我一直在做的是,当sax解析器无法使用endElement方法在第一个Ap对象中找到qName匹配项时,引发异常。然后我捕获异常并尝试ApWithVendor对象 我的问题是,有没有更好的方法来做到这一点,而不必对异常进行条件检查 代码示例 try { System.out.pri

我正在grails应用程序中创建一个xml文件上传程序。有两种类型的文件,Ap和ApWithVendor。我希望使用SAXParser自动检测文件类型并将xml转换为正确的对象

我一直在做的是,当sax解析器无法使用endElement方法在第一个Ap对象中找到qName匹配项时,引发异常。然后我捕获异常并尝试ApWithVendor对象

我的问题是,有没有更好的方法来做到这一点,而不必对异常进行条件检查

代码示例

        try {
            System.out.println("ApBatch");
            Batch<ApBatchEntry> batch = new ApBatchConverter().convertFromXML(new String(xmlDocument, StandardCharsets.UTF_8));

            byte[] xml = new ApBatchConverter().convertToXML(batch, true);
            String xmlString = new String(xml, StandardCharsets.UTF_8);
            System.out.println(xmlString);

            errors = client.validateApBatch(batch);
            if (!errors.isEmpty()) {
                throw new BatchValidationException(errors);
            }

            return;
        } catch (BatchConverterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            System.out.println("ApVendorBatch");
            Batch<ApWithVendorBatchEntry> batch = new ApWithVendorBatchConverter().convertFromXML(new String(xmlDocument, StandardCharsets.UTF_8));

            byte[] xml = new ApWithVendorBatchConverter().convertToXML(batch, true);
            String xmlString = new String(xml, StandardCharsets.UTF_8);
            System.out.println(xmlString);

            errors = client.validateApWithVendorBatch(batch);
            if (!errors.isEmpty()) {
                throw new BatchValidationException(errors);
            }

            return;
        } catch (BatchConverterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
试试看{
系统输出打印项次(“ApBatch”);
Batch Batch=new ApBatchConverter().convertFromXML(新字符串(xmlDocument,StandardCharsets.UTF_8));
byte[]xml=new ApBatchConverter().convertoxml(批处理,true);
String xmlString=新字符串(xml,StandardCharsets.UTF_8);
System.out.println(xmlString);
错误=client.validateAppatch(批处理);
如果(!errors.isEmpty()){
抛出新的BatchValidationException(错误);
}
返回;
}捕获(BatchConverterException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
System.out.println(“ApVendorBatch”);
Batch Batch=new ApWithVendorBatchConverter().convertFromXML(新字符串(xmlDocument,StandardCharsets.UTF_8));
字节[]xml=new ApWithVendorBatchConverter().convertToXML(批处理,true);
String xmlString=新字符串(xml,StandardCharsets.UTF_8);
System.out.println(xmlString);
错误=client.validateAppWithVendorBatch(批处理);
如果(!errors.isEmpty()){
抛出新的BatchValidationException(错误);
}
返回;
}捕获(BatchConverterException e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}

首先尝试将XML字符串转换为XML树对象,并使用XPath确定它是否为ApWithVendor结构。即,检查结构中是否存在类似“/application/foo/vendor”路径的元素。
一旦决定,请将XML树对象转换为对象。

首先尝试将XML字符串转换为XML树对象,然后使用XPath确定它是否为ApWithVendor结构。即,检查结构中是否存在类似“/application/foo/vendor”路径的元素。
一旦决定,将XML树对象转换为对象。

您可以始终迭代XML中的节点,并根据特定节点缺失(或存在-或具有特定值)这一事实做出决定(请参见
DocumentBuilder
Document
类)


在99%的情况下使用异常进行决策或流控制被认为是不好的做法。

您始终可以在XML中的节点上进行迭代,并根据特定节点缺失(或存在-或具有特定值)的事实进行决策(请参见
DocumentBuilder
Document
类)


在99%的情况下使用异常进行决策或流量控制被认为是不好的做法。

我同意这是一种不好的做法,从一开始就似乎是错误的,这让我想到了这个问题。谢谢,我会试一试。我同意这是一种不好的做法,只是从一开始它就似乎错了,这让我想到了这个问题。谢谢,我会试一试的。