How can I parse through XML response

dave_edgar
Mega Guru

I'm having real issues trying to find how to do this.   What I am doing is triggering a rest/http call and getting back an xml response of various data, hotel availability, and I get this sort of xml reply, it always comes back as xml even though the accept is set to application/json.

Here is an example of the responseBody:

<?xml version="1.0"?>

<OTA_HotelAvailRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EchoToken="" TimeStamp="2018-01-05T16:10:34.4052066+00:00" Version="0" xmlns="http://www.opentravel.org/OTA/2003/05">

  <Success />

  <RoomStays>

      <RoomStay>

          <RatePlans>

              <RatePlan RatePlanCode="d8f4159d-bb1a-4671-b2cc-23c32f63cc9b" />

          </RatePlans>

          <RoomRates>

              <RoomRate>

                  <Rates>

                      <Rate EffectiveDate="2018-01-05">

                          <Base AmountBeforeTax="00.00" AmountAfterTax="00.00" CurrencyCode="EUR" />

                          <RateDescription>

                              <Text>Bsi Booking Service. Standard Room With King-size Bed</Text>

                          </RateDescription>

                      </Rate>

                  </Rates>

              </RoomRate>

          </RoomRates>

          <BasicPropertyInfo HotelCode="26129" />

          <TPA_Extensions>

              <CustomerRateInformation>

                  <RateCap>0.00</RateCap>

              </CustomerRateInformation>

          </TPA_Extensions>

      </RoomStay>

      <RoomStay>

          <RatePlans>

              <RatePlan RatePlanCode="50be4dc0-acc8-43b3-8630-20226c84d5f0" />

          </RatePlans>

          <RoomRates>

              <RoomRate>

                  <Rates>

                      <Rate EffectiveDate="2018-01-05">

                          <Base AmountBeforeTax="00.00" AmountAfterTax="00.00" CurrencyCode="EUR" />

                          <RateDescription>

                              <Text>Bsi Booking Service. Standard Room 2 Single Beds</Text>

                          </RateDescription>

                      </Rate>

                  </Rates>

              </RoomRate>

          </RoomRates>

          <BasicPropertyInfo HotelCode="26129" />

          <TPA_Extensions>

              <CustomerRateInformation>

                  <RateCap>0.00</RateCap>

              </CustomerRateInformation>

          </TPA_Extensions>

      </RoomStay>

  </RoomStays>

</OTA_HotelAvailRS>

So I want to be able to do is parse though the xml response and split out in an array at certain points.   So all I want to take out are these lines:

<RatePlan RatePlanCode="50be4dc0-acc8-43b3-8630-20226c84d5f0" />

<Base AmountBeforeTax="00.00" AmountAfterTax="00.00" CurrencyCode="EUR" />

<Text>Bsi Booking Service. Standard Room With King-size Bed</Text>

I can get just the first <RatePlan RatePlanCode="50be4dc0-acc8-43b3-8630-20226c84d5f0" /> line out using this

var xmldoc = new XMLDocument(responseBody);  

var RatePlanCodeIs = xmldoc.getNodeText("//RatePlan/@RatePlanCode");

// var RatePlanCodeIsSplit = splitString(xmldoc.getNodeText("//RatePlan/@RatePlanCode"));

gs.info("rateplan::" + RatePlanCodeIs );

Logs out as rateplan::50be4dc0-acc8-43b3-8630-20226c84d5f0

BUT there is more then one of these so I want all of them so I can add them to a drop down box/addOption

I've tried all sorts from various post in the community and outside but can't seem to find something that works

Help

1 REPLY 1

Jim60
Tera Expert

JSON is a lot easier to work with in javascript, I would suggest converting it to JSON.


Processing Inbound XML - Scoped Application Strategy