私はgrailsアプリケーションで単一のxmlファイルアップローダーを作成しています。 ApとApWithVendorの2種類のファイルがあります。私は自動的にファイルの種類を検出し、SAXParserを使用して正しいオブジェクトにXMLを変換したいと思います。条件チェックにjava例外を使用する
私が行ってきたことは、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();
}
私はそれが悪い習慣であることに同意します。私はその質問に私を導く最初から間違っているようでした。ありがとう、私はそのショットを与えるでしょう。 –