首页 > 解决方案 > 从 xml 内容替换字符串

问题描述

我正在尝试从现有的 xml 字符串中替换整个 xml 字符串。但无论如何它都不起作用。我的php代码如下:

$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;

 ?>

标签: phpxml

解决方案


这个例子很长,但它是一次设置单个部分的情况。添加元素然后是元素的属性是很多重复的代码。您可能将所有这些数据分开(您当前$updated_string从这些数据构建 XML)。

$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();

推荐阅读