Je crée un seul téléchargeur de fichier xml dans mon application Grails. Il existe deux types de fichiers, Ap et ApWithVendor. Je voudrais détecter automatiquement le type de fichier et convertir le xml en l'objet correct en utilisant SAXParser. Ce que j'ai fait jette une exception quand l'analyseur de sax n'est pas capable de trouver une correspondance qName dans le premier objet Ap en utilisant la méthode endElement. J'attrape alors l'exception et j'essaie l'objet ApWithVendor.Utilisation de l'exception Java pour la vérification des conditions
Ma question est là une meilleure façon de faire ceci sans vérifier ma condition avec des exceptions?
Exemple de code
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();
}
Je suis d'accord pour dire que c'est une mauvaise pratique, cela m'a paru erroné dès le début, ce qui m'a conduit à la question. Merci, je vais donner un coup de feu. –