Girish Bellamkonda
Girish Bellamkonda

Reputation: 501

Retrieve node value from XML response in SOAPUI which contains CDATA tag

I am trying to retrieve the Atomic Number using Xpath from the below xml SoapUI response

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetAtomicNumberResponse xmlns="http://www.webserviceX.NET">
         <GetAtomicNumberResult><![CDATA[<NewDataSet>
  <Table>
    <AtomicNumber>47</AtomicNumber>
    <ElementName>Silver</ElementName>
    <Symbol>Ag</Symbol>
    <AtomicWeight>107.87</AtomicWeight>
    <BoilingPoint>2485</BoilingPoint>
    <IonisationPotential>7.58</IonisationPotential>
    <EletroNegativity>1.42</EletroNegativity>
    <AtomicRadius>1.34</AtomicRadius>
    <MeltingPoint>1235</MeltingPoint>
    <Density>10490</Density>
  </Table>
</NewDataSet>]]></GetAtomicNumberResult>
      </GetAtomicNumberResponse>
   </soap:Body>
</soap:Envelope>

have tried the below one

declare namespace ns2='http://www.webserviceX.NET';
//ns2:GetAtomicNumberResponse[1]/ns2:GetAtomicNumberResult[1]

am getting all the data starting from [<NewDataset> .... </NewDataSet>], but I just need <AtomicNumber> value.

Upvotes: 3

Views: 2314

Answers (3)

albciff
albciff

Reputation: 18507

The problem here is that CDATA is treated as string inside an Xml and when working as a XPath; so if you want to access a node inside CDATA using XPath you need first to make the path to access CDATA and a way to parse its content as an Xml.

Hopefully SOAPUI use Saxon, and Saxon contains the follow function saxon:parse which allows to parse the string content returned from XPath as a Xml.

Note that Saxon version 9.3 replace this function for saxon:parse-xml however in SOAPUI is not accessible since it use saxon version 9.1.8.

So in your Property transfer testStep you can use the follow XPath:

declare namespace ns2='http://www.webserviceX.NET';
(saxon:parse(//ns2:GetAtomicNumberResponse[1]/ns2:GetAtomicNumberResult[1]))//*:AtomicNumber

Or in a shorter SOAPUI way:

(saxon:parse(//*:GetAtomicNumberResponse/*:GetAtomicNumberResult))//*:AtomicNumber

enter image description here

Upvotes: 3

Rajeev Kumar
Rajeev Kumar

Reputation: 1

def respXmlHolder = new XmlHolder(testRunner.testCase.testSteps["GetAtomicNumber"].testRequest.response.getContentAsXml());

respXmlHolder.namespaces["ns"] = "http://www.webserviceX.NET";

def CDATAXml = respXmlHolder.getNodeValue("//ns:GetAtomicNumberResponse/ns:GetAtomicNumberResult");

log.info CDATAXml;

def CDATAXmlHolder = new XmlHolder(CDATAXml);

def atomicNumber=CDATAXmlHolder.getNodeValue("//NewDataSet/Table/AtomicNumber");

com.eviware.soapui.support.UISupport.showInfoMessage("Atomic Number:-"+atomicNumber);

log.info "Atomic Number:-"+atomicNumber

Upvotes: 0

Thakur
Thakur

Reputation: 557

c#

XDocument doc = XDocument.Load("XMLFile1.xml");
var result = doc.Descendants(XNamespace.Get("http://www.webserviceX.NET")+"AtomicNumber")
                .First();

Upvotes: -4

Related Questions