0

I am trying to replace a whole xml string from an existing xml string. but it's not working anyway. My php code is as follows:

$xml_str = '<air:AirPricingInfo xmlns:air="http://www.travelport.com/schema/air_v48_0" Key="lR26Z14R2BKAmzCGAAAAAA==" TotalPrice="BDT27754" BasePrice="USD234.00" ApproximateTotalPrice="BDT27754" ApproximateBasePrice="BDT19598" EquivalentBasePrice="BDT19598" Taxes="BDT8156" ApproximateTaxes="BDT8156" LatestTicketingTime="2020-01-06" PricingMethod="Guaranteed" ETicketability="Yes" PlatingCarrier="MH" ProviderCode="1G" Cat35Indicator="false">      <air:FareInfoRef Key="lR26Z14R2BKAlzCGAAAAAA=="/>      <air:FareInfoRef Key="lR26Z14R2BKAvzCGAAAAAA=="/>      <air:BookingInfo BookingCode="N" BookingCount="9" CabinClass="Economy" FareInfoRef="lR26Z14R2BKAlzCGAAAAAA==" SegmentRef="lR26Z14R2BKAWzCGAAAAAA=="/>      <air:BookingInfo BookingCode="N" BookingCount="9" CabinClass="Economy" FareInfoRef="lR26Z14R2BKAvzCGAAAAAA==" SegmentRef="lR26Z14R2BKAyzCGAAAAAA=="/>      <air:TaxInfo Category="BD" Amount="BDT500" Key="lR26Z14R2BKAnzCGAAAAAAAA"/>      <air:TaxInfo Category="E5" Amount="BDT75" Key="lR26Z14R2BKAozCGAAAAAAAA"/>      <air:TaxInfo Category="OW" Amount="BDT2000" Key="lR26Z14R2BKApzCGAAAAAAAA"/>      <air:TaxInfo Category="UT" Amount="BDT3000" Key="lR26Z14R2BKAqzCGAAAAAAAA"/>      <air:TaxInfo Category="G1" Amount="BDT407" Key="lR26Z14R2BKArzCGAAAAAAAA"/>      <air:TaxInfo Category="H8" Amount="BDT21" Key="lR26Z14R2BKAszCGAAAAAAAA"/>      <air:TaxInfo Category="MY" Amount="BDT1483" Key="lR26Z14R2BKAtzCGAAAAAAAA"/>      <air:TaxInfo Category="YQ" Amount="BDT670" Key="lR26Z14R2BKAuzCGAAAAAAAA"/>      <air:FareCalc>DAC MH KUL 111.00NBC6MSBD MH DAC 122.50NBC6MBD NUC233.50END ROE1.0</air:FareCalc>      <air:PassengerType Code="ADT"/>      <air:PassengerType Code="ADT"/>      <air:ChangePenalty PenaltyApplies="Anytime">        <air:Amount>BDT3350.0</air:Amount>      </air:ChangePenalty>      <air:CancelPenalty NoShow="true" PenaltyApplies="Anytime">        <air:Percentage>100.00</air:Percentage>      </air:CancelPenalty>    </air:AirPricingInfo>';
$string_to_be_replaced = '<air:FareInfoRef Key="lR26Z14R2BKAlzCGAAAAAA=="/>';
$updated_string = '<air:FareInfo Key="123456" FareBasis="XYZ456" PassengerTypeCode="ADT" Origin="DAC" Destination="KUL" EffectiveDate="2019-09-23T09:04:00.000+06:00" DepartureDate="2020-01-06" Amount="BDT9297" NegotiatedFare="false" NotValidBefore="2020-01-06" NotValidAfter="2020-01-06"><air:BaggageAllowance><air:MaxWeight Value="25" Unit="Kilograms"/></air:BaggageAllowance>   <air:FareRuleKey FareInfoRef="lR26Z14R2BKAlzCGAAAAAA==" ProviderCode="1G">gws-eJxNjrEOwjAMRD+muv18QKFb2oSqEjQLdOjC/38GTlIkLNk++1m2QwiiDRx0Cv/W4dOtC/IWgQy5P7YnzIyEebWD5A15iv36mhJEkcbeUa64ZauDUfHsCbNmNlQMe41pjM7aZt9pKKdRpDfwE/fFZR7Te0oql2m6SAfkFf7rF9LkK5E=</air:FareRuleKey><air:Brand Key="lR26Z14R2BKAX0CGAAAAAA==" BrandID="208719" BrandTier="0001"/></air:FareInfo>';
$new_string = str_replace($string_to_be_replaced,$updated_string,$xml_str);
echo $new_string;

 ?>
Enayet Hossain
  • 187
  • 1
  • 4
  • 14
  • 1
    You would probably be better off trying to use a proper XML method than string manipulation like this. Minor changes to the source document will mean this can fail at any point. – Nigel Ren Nov 16 '19 at 10:03
  • Can u pls give me an example regarding my expected output? Thanks in advance – Enayet Hossain Nov 16 '19 at 11:43
  • Does this answer your question? [How do you parse and process HTML/XML in PHP?](https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – miken32 Nov 17 '19 at 05:23

1 Answers1

0

This example is quite long, but it's a case of setting the individual parts one at a time. It is a lot of repeated code for adding elements and then the attributes of the elements. You may have all of this data separate (which you currently build the XML $updated_string from).

$doc = new DOMDocument();
$doc->loadXML($xml_str);
$airNS = "http://www.travelport.com/schema/air_v48_0";
$xp = new DOMXPath($doc);

// Find the elements to be replaced
$toBeReplaced = $xp->query('//air:FareInfoRef[@Key]');

foreach ( $toBeReplaced as $updateNode )   {
    $key = $updateNode->getAttribute("Key");
    $updateNode->removeAttribute("Key");
    $updateNode->setAttribute("Key", "123456");
    $updateNode->setAttribute("FareBasis", "XYZ456");
    $updateNode->setAttribute("PassengerTypeCode", "ADT");
    $updateNode->setAttribute("Origin", "DAC");
    $updateNode->setAttribute("Destination", "KUL");
    $updateNode->setAttribute("EffectiveDate", "2019-09-23T09:04:00.000+06:00");
    $updateNode->setAttribute("DepartureDate", "2020-01-06");
    $updateNode->setAttribute("Amount", "BDT9297");
    $updateNode->setAttribute("NegotiatedFare", "false");
    $updateNode->setAttribute("NotValidBefore", "2020-01-06");
    $updateNode->setAttribute("NotValidAfter", "2020-01-06");
    $bagAll = $doc->createElementNS($airNS, "BaggageAllowance");
    $bagWeight = $doc->createElementNS($airNS, "MaxWeight");
    $bagWeight->setAttribute("Value", "25");
    $bagWeight->setAttribute("Unit", "Kilograms");
    $bagAll->appendChild($bagWeight);
    $updateNode->appendChild($bagAll);
    $fareRuleKey = $doc->createElementNS($airNS, "FareRuleKey",
        "gws-eJxNjrEOwjAMRD+muv18QKFb2oSqEjQLdOjC/38GTlIkLNk++1m2QwiiDRx0Cv/W4dOtC/IWgQy5P7YnzIyEebWD5A15iv36mhJEkcbeUa64ZauDUfHsCbNmNlQMe41pjM7aZt9pKKdRpDfwE/fFZR7Te0oql2m6SAfkFf7rF9LkK5E=");
    $fareRuleKey->setAttribute("FareInfoRef", $key);
    $fareRuleKey->setAttribute("ProviderCode", "1G");
    $updateNode->appendChild($fareRuleKey);
    $brand = $doc->createElementNS($airNS, "Brand");
    $brand->setAttribute("Key", $key);
    $brand->setAttribute("BrandID", "208719");
    $brand->setAttribute("BrandTier", "0001");
    $updateNode->appendChild($brand);
}
echo $doc->saveXML();
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • no.. if i echo $doc->saveXML(); it just print the value... not the xml content itself..``` gws-eJxNjrEOwjAMRD+muv18QKFb2oSqEjQLdOjC/38GTlIkLNk++1m2QwiiDRx0Cv/W4dOtC/IWgQy5P7YnzIyEebWD5A15iv36mhJEkcbeUa64ZauDUfHsCbNmNlQMe41pjM7aZt9pKKdRpDfwE/fFZR7Te0oql2m6SAfkFf7rF9LkK5E= gws-eJxNjrEOwjAMRD+muv18QKFb2oSqEjQLdO ``` – Enayet Hossain Nov 17 '19 at 10:26
  • If you are doing this to a browser, then view the source of the document, or save it to a file. – Nigel Ren Nov 17 '19 at 10:32