XML

29 views
Skip to first unread message

Pascal Collard

unread,
Mar 22, 2024, 3:30:46 AMMar 22
to REST assured

could any one point out why I get my XML's full values of all elements concatenated when I use an xpath that is not matching an element in the XML ? I would expect an exception (i.e.: element not found) or a null value rather, am I missing something ?

XML example:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rootElement>
     <topElement1>value 1</topElement1>
     <topElement2>value 2</topElement2>
     <topElement3 attribute="att3">
          <childElementA>value A</childElementA>
          <childElementB attribute="attB">value B</childElementB>
     </topElement3>
</rootElement>


Java:

String responseBody = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><rootElement><topElement1>value 1</topElement1><topElement2>value 2</topElement2><topElement3 attribute=\"att3\"><chileElementA>value A</chileElementA><chileElementB attribute=\"attB\">value A</chileElementA></topElement3></rootElement>";
String actual = new XmlPath(responseBody).get(xpath);


If I then call this code with different xpath which are not 'valid', the returned value is the text of all elements in the XML document.
Examples:

new XmlPath(responseBody).getString("topElement1") -> "value 1value 2value Avalue B"
new XmlPath(responseBody).getString("nonExisting") -> "value 1value 2value Avalue B"


Unless I set a 'valid' path, of an existing element, then it returns only that element's value.
Examples:

new XmlPath(responseBody).getString("rootElement.topElement1") -> "value 1
new XmlPath(responseBody).getString("
rootElement.topElement3.childElementB" ) -> "value B"


Why an xpath that is not matching an element returns the whole document's elements texts ?
Thanks

PS: with a json file and the Rest Assured's json library, if the element is not found based on the xpath, an exception would be returned.

Reply all
Reply to author
Forward
0 new messages