Je rencontre un problème lors de la comparaison de divers éléments de mon document XML avec XMLUnit (2.2.1). Dans mon document il y a plusieurs éléments xml et je veux savoir, s'ils diffèrent les uns des autres. Cependant, je ne veux pas comparer tous les éléments xml de la même manière. Parfois, je veux juste les comparer par leur nom. Dans d'autres cas, je veux les comparer par nom et attribut ou par nom et texte.Unité XML - Utilisation de sélecteurs d'éléments personnalisés sur différents éléments xml
Voici un exemple (voir les commentaires)
contrôle
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<LANGUAGES>
<!-- Compare LANGUAGE by Name and Attribute -->
<LANGUAGE VALUE="DE" />
<LANGUAGE VALUE="EN" />
<LANGUAGE VALUE="IT" />
<LANGUAGE VALUE="FR" />
</LANGUAGES>
<CODES>
<!-- Compare CODE by Name and Text -->
<CODE>10000-1</CODE>
<CODE>20000-2</CODE>
<CODE>30000-3</CODE>
<CODE>40000-4</CODE>
</CODES>
<CONTACT> <!-- Compare CONTACT and Children just by Name -->
<FIRSTNAME>Max</FIRSTNAME>
<SURNAME>Mustermann</SURNAME>
</CONTACT>
</ROOT>
Test:
<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<LANGUAGES>
<!-- Compare LANGUAGE by Name and Attribute -->
<LANGUAGE VALUE="DE" />
<LANGUAGE VALUE="FR" />
</LANGUAGES>
<CODES>
<!-- Compare CODE by Name and Text -->
<CODE>20000-2</CODE>
<CODE>40000-4</CODE>
</CODES>
<CONTACT> <!-- Compare CONTACT and Children by Name -->
<FIRSTNAME>Tim</FIRSTNAME>
<SURNAME>Mustermann</SURNAME>
</CONTACT>
</ROOT>
J'ai essayé ElementSelectors en combinaison avec ElementSelectors.conditionalBuilder (https://github.com/xmlunit/user-guide/wiki/SelectingNodes#conditional-elementselectors) à appliquer seulement une ElementSelector sur un élément spécifique (whenElementIsNamed). Peut-être que ce n'est pas la bonne approche pour ce que je veux réaliser.
Ceci est mon code que j'utilise pour le test:
public void xmlDiff() {
String control = getControlDocument(); //
String test = getTestDocument(); //
Diff myDiff = DiffBuilder.compare(control)//
.withTest(test) //
.ignoreWhitespace() //
.ignoreComments() //
.checkForSimilar() //
.withNodeMatcher(new DefaultNodeMatcher(partialElementSelector("LANGUAGE", ElementSelectors.byNameAndAllAttributes), partialElementSelector("CODE",ElementSelectors.byNameAndText), ElementSelectors.byName)) //
.build();
assertThat(myDiff.hasDifferences()).isTrue(); //
}
private ElementSelector partialElementSelector(final String expectedName, final ElementSelector elementSelector) {
return ElementSelectors.conditionalBuilder().whenElementIsNamed(expectedName).thenUse(elementSelector).build();
}
Ce que je réellement besoin est l'information que deux langues (FR, IT) et deux codes (10000-1,30000-3) ont été supprimé (non remplacé) et FIRSTNAME modifié.
Comment puis-je obtenir ces informations, avec ou sans unité XML (DiffBuilder)?
Nous vous remercions de votre aide!
Merci Stefan, c'est ce que je cherchais :)! – fwshngtn
J'ai ajouté https://github.com/xmlunit/user-guide/wiki/SelectingNodes#using-more-than-one-elementselector-at-the-same-time –
Heureux qu'il fonctionne pour vous @fwshngtn –