2017-09-05 1 views
0

J'ai une chaîne xml et je veux comparer seulement les valeurs de noeuds sélectionnées et affirmer mon test sur le résultat. Par exemple: J'ai la chaîne XML suivantComment faire correspondre uniquement les valeurs de noeud sélectionnées d'un fichier XML?

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
    <soap:Body> 
     <ns2:requestTransferResponse xmlns:ns2="http://external.interfaces.my.company.com/"> 
     <return> 
      <ersReference>2017051017472278401000061</ersReference> 
      <resultCode>0</resultCode> 
      <resultDescription>SUCCESS</resultDescription> 
      <receivedAmount> 
       <currency>BSD</currency> 
       <value>5500.00</value> 
      </receivedAmount> 
      <receiverPrincipal> 
       <principalId> 
        <id>SUBDIST1-1</id> 
        <type>RESELLERID</type> 
       </principalId> 
       <principalName>Sub Distributor 1</principalName> 
       <accounts> 
        <account> 
        <accountSpecifier> 
         <accountId>SUBDIST1-1</accountId> 
         <accountTypeId>RESELLER</accountTypeId> 
        </accountSpecifier> 
        <balance> 
         <currency>BSD</currency> 
         <value>985500.00</value> 
        </balance> 
        <creditLimit> 
         <currency>BSD</currency> 
         <value>0.00000</value> 
        </creditLimit> 
        </account> 
       </accounts> 
       <status>Active</status> 
       <msisdn>12420101000</msisdn> 
      </receiverPrincipal> 
      <requestedTransferAmount> 
       <currency>BSD</currency> 
       <value>5500</value> 
      </requestedTransferAmount> 
      <senderPrincipal> 
       <principalId> 
        <id>DIST1</id> 
        <type>RESELLERID</type> 
       </principalId> 
       <principalName>Distributor 1</principalName> 
       <accounts> 
        <account> 
        <accountSpecifier> 
         <accountId>DIST1</accountId> 
         <accountTypeId>RESELLER</accountTypeId> 
        </accountSpecifier> 
        <balance> 
         <currency>BSD</currency> 
         <value>1994429.24</value> 
        </balance> 
        <creditLimit> 
         <currency>BSD</currency> 
         <value>0.00000</value> 
        </creditLimit> 
        </account> 
       </accounts> 
       <status>Active</status> 
       <msisdn>12420100000</msisdn> 
      </senderPrincipal> 
     </return> 
     </ns2:requestTransferResponse> 
    </soap:Body> 
</soap:Envelope> 

Maintenant, je veux valider les valeurs des nœuds suivants ayant XPath

1. //receiverPrincipal//balance//value i.e 985500.00 
2. //senderPrincipal//balance//value i.e. 1994429.24 

reste des noeuds ne sont pas nécessaires. J'ai donc cherché sur Internet et trouvé la bibliothèque XmlUnit. Quelqu'un peut-il donner un indice sur la façon dont je peux utiliser ceci pour valider les valeurs de nœuds xpath?

Répondre

0

Si vous avez seulement besoin de quelques textes sélectionnés que vous pouvez identifier par XPath, le support XPath de XMLUnit devrait fonctionner.

Traduire l'exemple de la page GitHub de xmlunit (voir https://github.com/xmlunit/xmlunit/#asserting-an-xpath-value) à votre première exigence

XPathEngine xpath = new JAXPXPathEngine(); 
String content = xpath.evaluate("//receiverPrincipal//balance/value/text()", source); 
assertEquals(985500.00, Double.valueOf(content), 1e-3); 

si vous êtes vraiment sûr qu'il y aura qu'un seul match pour le XPath.

Utilisation du Hamcrest matcher ce serait quelque chose comme

assertThat(source, EvaluateXPathMatcher.hasXPath("//receiverPrincipal//balance/value/text(), 
    equalTo("985500.00"))