简体   繁体   English

使用Java异常进行条件检查

[英]Using java exception for condition checking

I'm creating a single xml file uploader in my grails application. 我在grails应用程序中创建了一个xml文件上传器。 There is two types of files, Ap and ApWithVendor. 有两种类型的文件,Ap和ApWithVendor。 I would like to auto detect the file type and convert the xml to the correct object using SAXParser. 我想自动检测文件类型,并使用SAXParser将xml转换为正确的对象。

What I've been doing is throwing an exception when the sax parser is unable to find a qName match within the the first Ap object using the endElement method. 当sax解析器无法使用endElement方法在第一个Ap对象内找到qName匹配项时,我一直在抛出异常。 I then catch the exception and try the the ApWithVendor object. 然后,我捕获异常并尝试ApWithVendor对象。

My question is there a better way to do this without doing my condition checking with exceptions? 我的问题是有一种更好的方法来执行此操作,而不进行异常条件检查吗?

Code example 代码示例

        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();
        }

Try converting the XML string to an XML tree object first and use XPath to decide if it's an ApWithVendor structure. 尝试先将XML字符串转换为XML树对象,然后使用XPath来确定它是否为ApWithVendor结构。 Ie check if there is an element like "/application/foo/vendor" path in the structure. 即检查结构中是否有类似“ / application / foo / vendor”路径的元素。 Once you have decided, convert the XML tree object to an object. 确定后,将XML树对象转换为对象。

You can always iterate over the nodes in the XML and base decision on the fact that specific Node is missing (or is present - or has specific value) (see DocumentBuilder and Document class) 您始终可以遍历XML中的节点,并根据特定节点缺失(或存在-或具有特定值)这一事实来做出决定(请参阅DocumentBuilderDocument类)

Using exceptions for decision-making or flow-control in 99% situations is considered bad practice. 在99%的情况下将异常用于决策或流控制被认为是不良做法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM