4 Replies · Latest reply on Jan 14, 2018 12:38 PM by Dave Edgar

    XML array issue

      bawiskar i came across your answer to thread Using XPATH to get multiple values from the same node name and I think you may hold the key to my problem

       

      This is the XML I am getting:

       

      and here is the script I am using BUT there are multiple lines i need to get, a good example is "RatePlan RatePlanCode" which there are several of throughout the XML, which I need to display from the array.

      Script:

      (function executeRule(current, previous /*null when async*/) {

      var xmlString = current.getDisplayValue('u_api_feedback_1');

      gs.info("current_uapifeedback1::" + xmlString);

      var k=0;

      var arr=[];

      var xmldoc = new XMLDocument(xmlString);

      var noParamNode = xmldoc.getNode("//RoomStay/*").getLength();

      //getNodeText("//RatePlan/@RatePlanCode");

      for(var k=1; k<noParamNode+1; k++){

      var value = xmldoc.getNodeText("//["+k+"]");

      gs.info('Individual value for string tag is::'+value);

      arr.push(value.toString());    }

      gs.info('Array is::'+arr);

      current.setValue('u_api_feedback_2', ('Array is::'+arr));  

      })(current, previous);

       

      XML / Field u_api_feedback_1

      <?xml version="1.0"?>

      <OTA_HotelAvailRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EchoToken="ABC123" TimeStamp="2018-01-08T10:38:57.2965586+00:00" Version="0" xmlns="http://www.opentravel.org/OTA/2003/05">

        <Success />

        <RoomStays>

          <RoomStay>

            <RatePlans>

              <RatePlan RatePlanCode="f45d123c-8c40-44e9-9a8b-08e379cbdf3f" />

            </RatePlans>

            <RoomRates>

              <RoomRate>

                <Rates>

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

                    <Base AmountBeforeTax="134.00" AmountAfterTax="134.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="0d8d212c-ef89-40bb-8ee5-3d13e5d754d1" />

            </RatePlans>

            <RoomRates>

              <RoomRate>

                <Rates>

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

                    <Base AmountBeforeTax="134.00" AmountAfterTax="134.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>

          <RoomStay>

            <RatePlans>

              <RatePlan RatePlanCode="f64c0322-66b5-4f4a-ba99-3ee12dbbec15" />

            </RatePlans>

            <RoomRates>

              <RoomRate>

                <Rates>

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

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

                    <RateDescription>

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

                    </RateDescription>

                  </Rate>

                </Rates>

              </RoomRate>

            </RoomRates>

            <BasicPropertyInfo HotelCode="26129" />

            <TPA_Extensions>

              <CustomerRateInformation>

                <RateCap>0.00</RateCap>

              </CustomerRateInformation>

            </TPA_Extensions>

          </RoomStay>

        </RoomStays>

      </OTA_HotelAvailRS>

       

      Can you help be rip out the info from the nodes?  I'd need these for example

      RatePlan RatePlanCode

      Base AmountBeforeTax

      Text

      HotelCode

      Dave

        • Re: XML array issue
          Ankur Bawiskar

          Hi Dave,

           

          Sure I will check on this and let you know.

           

          Regards

          Ankur

          • Re: XML array issue
            Ankur Bawiskar

            Hi Dave,

             

            I was able to retrieve the values you required from the xml string.

            I ran this script in scripts background and printed the result. I have used 4 arrays to store the values one each for RatePlanCode, AmountBeforeTax, Text and HotelCode.

             

            Note:

            RatePlanCode, AmountBeforeTax and HotelCode are attributes of xml tag and we have fetched value of that attribute

            Text is the xml tag and we have fetched value of the xml tag and not attribute

             

            Note:

            This script ran in global scope

            For testing purpose I changed the hotelCode value to 26129, 26130, 26131 in the sample xml string to ensure whether all values are coming in the array because hotel code was 26129 at all the 3 places.

             

            you can try running this script and check the result.

             

            var RatePlanCode = [];

            var AmountBeforeTax = [];

            var Text = [];

            var HotelCode = [];

             

            var xmlstring = '<?xml version="1.0"?><OTA_HotelAvailRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EchoToken="ABC123" TimeStamp="2018-01-08T10:38:57.2965586+00:00" Version="0" xmlns="http://www.opentravel.org/OTA/2003/05"><Success /><RoomStays><RoomStay><RatePlans><RatePlan RatePlanCode="f45d123c-8c40-44e9-9a8b-08e379cbdf3f" /></RatePlans><RoomRates><RoomRate><Rates><Rate EffectiveDate="2018-01-08"><Base AmountBeforeTax="134.00" AmountAfterTax="134.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="0d8d212c-ef89-40bb-8ee5-3d13e5d754d1" /></RatePlans><RoomRates><RoomRate><Rates><Rate EffectiveDate="2018-01-08"><Base AmountBeforeTax="134.00" AmountAfterTax="134.00" CurrencyCode="EUR" /><RateDescription><Text>Bsi Booking Service. Standard Room 2 Single Beds</Text></RateDescription></Rate></Rates></RoomRate></RoomRates><BasicPropertyInfo HotelCode="26130" /><TPA_Extensions><CustomerRateInformation><RateCap>0.00</RateCap></CustomerRateInformation></TPA_Extensions></RoomStay><RoomStay><RatePlans><RatePlan RatePlanCode="f64c0322-66b5-4f4a-ba99-3ee12dbbec15" /></RatePlans><RoomRates><RoomRate><Rates><Rate EffectiveDate="2018-01-08"><Base AmountBeforeTax="144.00" AmountAfterTax="144.00" CurrencyCode="EUR" /><RateDescription><Text>Bsi Booking Service. Superior Room With 1 King-size Bed</Text></RateDescription></Rate></Rates></RoomRate></RoomRates><BasicPropertyInfo HotelCode="26131" /><TPA_Extensions><CustomerRateInformation><RateCap>0.00</RateCap></CustomerRateInformation></TPA_Extensions></RoomStay></RoomStays></OTA_HotelAvailRS>';

             

            var xmldoc = new XMLDocument(xmlstring);

            var noParamNode = xmldoc.getNodes("/OTA_HotelAvailRS/RoomStays/RoomStay").getLength();

             

            for(var k=1; k<noParamNode+1; k++){

             

            var ratePlanCodeValue = xmldoc.getAttribute("//RoomStays[1]/RoomStay["+k+"]/RatePlans[1]/RatePlan[1]", "RatePlanCode");

            RatePlanCode.push(ratePlanCodeValue.toString());

            var amountBeforeTax = xmldoc.getAttribute("//RoomStays[1]/RoomStay["+k+"]/RoomRates[1]/RoomRate[1]/Rates[1]/Rate[1]/Base[1]", "AmountBeforeTax");

            AmountBeforeTax.push(amountBeforeTax.toString());

            var textValue = xmldoc.getNodeText("//RoomStays[1]/RoomStay["+k+"]/RoomRates[1]/RoomRate[1]/Rates[1]/Rate[1]/RateDescription[1]/Text");

            Text.push(textValue.toString());

            var hotelCode = xmldoc.getAttribute("//RoomStays[1]/RoomStay["+k+"]/BasicPropertyInfo[1]", "HotelCode");

            HotelCode.push(hotelCode.toString());

            }

             

            gs.print('RatePlanCode array is::' + RatePlanCode);

            gs.print('AmountBeforeTax array is::' + AmountBeforeTax);

            gs.print('Text array is::' + Text);

            gs.print('HotelCode array is::' + HotelCode);

             

            Screenshot of ouput:

            xml parsing complex stuff.JPG

             

            Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact. Also endorse the content if you feel so.

            Thanks

            Ankur

            • Re: XML array issue
              Dave Edgar

              Thankyou so much. I’ll try it as soon as I het back in the office on Monday. 

              Again thank you

              Dave