> For the complete documentation index, see [llms.txt](https://jen-hsuan-hsieh.gitbook.io/asp-net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jen-hsuan-hsieh.gitbook.io/asp-net/chapter4rest-apiweb-services-testing-with-soapui/43assertion/435handling-cdata-using-xpath-and-groovy.md).

# 4.3.5.Handling CDATA - using XPath and Groovy

## 1. CDATA

* 嵌在XML裡面的區塊, 可以是XML, 也可以是其他資訊&#x20;
* 搭配Groovy來parsing
* 無法用一般的方式parsing

## 2.Parsing CDATA

* 1.Add Assertion-> Property Content -> XPath Match ![](/files/-M4M0ZROtc8WMmgxfofV)
* 2.點選Declare
* 3.填上路徑

  * e.g.,

  ```
        <getAirportInformationByAirportCodeResponse xmlns="http://www.webserviceX.NET">
             <getAirportInformationByAirportCodeResult><![CDATA[<?xml version="1.0" encoding="UTF-8"?><NewDataSet>
              <Table>
                <AirportCode>LHR</AirportCode>
                <CityOrAirportName>LONDON HEATHROW</CityOrAirportName>
                <Country>Great Britain (UK)</Country>
                <CountryAbbrviation>GB</CountryAbbrviation>
                <CountryCode>493</CountryCode>
                <GMTOffset>0</GMTOffset>
                <RunwayLengthFeet>12802</RunwayLengthFeet>
                <RunwayElevationFeet>80</RunwayElevationFeet>
                <LatitudeDegree>51</LatitudeDegree>
                <LatitudeMinute>28</LatitudeMinute>
                <LatitudeSecond>0</LatitudeSecond>
                <LatitudeNpeerS>N</LatitudeNpeerS>
                <LongitudeDegree>0</LongitudeDegree>
                <LongitudeMinute>27</Longitude
                <LongitudeSeconds>0</LongitudeSeconds>
                <LongitudeEperW>W</LongitudeEperW>
              </Table>
  ```

  * 應填入

  ```
  ```

## 3.Parsing CDATA by using Groovy

* 1.Add Assertion-> Property Content -> XPath Match ![](/files/-M4M0ZRQM3UnIOm2pDNU)
* 2.重新命名Test step: 改為getAirportInformationByAirportCode ![](/files/-M4M0ZRS-o-SW-ewQF1y)
* 3.編輯script
  \*

  1. context.expand放的字串順序為: Test step名稱#Response#declare...;//ns1

  *

  ```
  1. 用groovy.util.XmlSlurper來parse出想要得到的key
  ```

  ````
  ```
  import groovy.util.XmlSlurper
  //1. Extract CDATA from the response

  //declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/';
  //declare namespace ns1='http://www.webserviceX.NET';
  //ns1:getAirportInformationByAirportCodeResponse/ns1:getAirportInformationByAirportCodeResult/text()
  def responseText = context.expand('${getAirportInformationByAirportCode#Response#declare namespace       ns1=\'http://www.webserviceX.NET\';//ns1:getAirportInformationByAirportCodeResponse/ns1:getAirportInformationByAirportCodeResult/text()}')
  log.info responseText

  XmlSlurper xmlSlurper = new XmlSlurper()

  def xmlData = xmlSlurper.parseText(responseText)

  log.info xmlData

  //2. I want to Assert airportCode == 'LHR'
  log.info xmlData.Table[0].AirportCode

  String airportCode= xmlData.Table[0].AirportCode

  assert airportCode == 'LHR'

  log.info 'Assering done'
  ```
  ````
