Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 在php DOMXPath中删除和添加节点_Xml_Domxpath - Fatal编程技术网

Xml 在php DOMXPath中删除和添加节点

Xml 在php DOMXPath中删除和添加节点,xml,domxpath,Xml,Domxpath,我试图从给定的XML内容中删除某些节点。同时,我还想向现有xml中添加其他节点,例如: $xml_string = '<air:AirPricingInfo xmlns:air="http://www.travelport.com/schema/air_v48_0" Key="82won53R2BKAcqfvFAAAAA==" TotalPrice="BDT27725" BasePrice="USD234.00" ApproximateTotalPrice="BDT27725" Appro

我试图从给定的XML内容中删除某些节点。同时,我还想向现有xml中添加其他节点,例如:

$xml_string = '<air:AirPricingInfo xmlns:air="http://www.travelport.com/schema/air_v48_0" Key="82won53R2BKAcqfvFAAAAA==" TotalPrice="BDT27725" BasePrice="USD234.00" ApproximateTotalPrice="BDT27725" ApproximateBasePrice="BDT19598" EquivalentBasePrice="BDT19598" Taxes="BDT8127" ApproximateTaxes="BDT8127" LatestTicketingTime="2019-11-30T23:59:00.000+01:00" PricingMethod="Guaranteed" ETicketability="Yes" PlatingCarrier="MH" ProviderCode="1G" Cat35Indicator="false"><air:FareInfo FareBasis="NBC6MBD" PassengerTypeCode="ADT" Origin="KUL" Destination="DAC" EffectiveDate="2019-11-21T22:41:00.000+06:00" DepartureDate="2020-02-19" Amount="BDT10301" NegotiatedFare="false" Key="82won53R2BKAbqfvFAAAAA=="/>          <air:FareInfo FareBasis="NBC6MBD" PassengerTypeCode="ADT" Origin="KUL" Destination="DAC" EffectiveDate="2019-11-21T22:41:00.000+06:00" DepartureDate="2020-02-19" Amount="BDT10301" NegotiatedFare="false" Key="82won53R2BKAlqfvFAAAAA=="/><air:BookingInfo BookingCode="N" BookingCount="9" CabinClass="Economy" FareInfoRef="82won53R2BKAbqfvFAAAAA==" SegmentRef="82won53R2BKAMqfvFAAAAA=="/><air:BookingInfo BookingCode="X" BookingCount="5" CabinClass="Economy" FareInfoRef="82won53R2BKAbqfvFAAAAA==" SegmentRef="82won53R2BKAMqfvFBBB=="/><air:TaxInfo Category="BD" Amount="BDT500" Key="82won53R2BKAdqfvFAAAAAAA"/><air:TaxInfo Category="OW" Amount="BDT2000" Key="82won53R2BKAeqfvFAAAAAAA"/><air:TaxInfo Category="UT" Amount="BDT3000" Key="82won53R2BKAfqfvFAAAAAAA"/><air:TaxInfo Category="G1" Amount="BDT401" Key="82won53R2BKAgqfvFAAAAAAA"/><air:TaxInfo Category="H8" Amount="BDT20" Key="82won53R2BKAhqfvFAAAAAAA"/><air:TaxInfo Category="MY" Amount="BDT1461" Key="82won53R2BKAiqfvFAAAAAAA"/><air:TaxInfo Category="E5" Amount="BDT75" Key="82won53R2BKAjqfvFAAAAAAA"/><air:TaxInfo Category="YQ" Amount="BDT670" Key="82won53R2BKAkqfvFAAAAAAA"/><air:FareCalc>DAC MH KUL 111.00NBC6MSBD MH DAC 122.50NBC6MBD NUC233.50END ROE1.0</air:FareCalc>  <air:PassengerType Code="CNN"/><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>';
但它抛出了一个错误:“html_entity_decode()期望参数1是字符串,对象给定”。
实际上,我想将$test作为xml内容返回。

您的
html\u entity\u decode()
调用是有害的。该函数将html和xml实体转换为字符,但这是通过在内部将它们加载到DOM中完成的。因此,在最好的情况下,调用什么也不做,在最坏的情况下,它们破坏了XML

DOMXpath::query()
的返回值是一个节点列表,您将其视为一个字符串。根据Xpath表达式,只有
DOMXpath::evaluate()
可以返回标量值或节点列表。但是,如果要修改节点,则需要它们。使用
foreach
迭代节点列表

$document = new DOMDocument();
$document->loadXML($xml_string);
$document->preserveWhiteSpace = FALSE;
$xpath = new DOMXpath($document);

$airNS = "http://www.travelport.com/schema/air_v48_0";
// register the namspace for xpath expressions
$xpath->registerNamespace('air', $airNS);

// fetch matching nodes and delete them
foreach ($xpath->evaluate('//air:BookingInfo[@SegmentRef="82won53R2BKAMqfvFBBB=="]') as $bookingInfo) {
    $bookingInfo->parentNode->removeChild($bookingInfo);
}
// look for the passenger info
foreach ($xpath->evaluate('//air:PassengerType[@Code="CNN"]') as $passengerType) {
    // change the attribute
    $passengerType->setAttribute('BookingTravelerRef', 'child_1');
    // insert the new node after the current one
    $passengerType->parentNode->insertBefore(
        // create a new element node in the namespace
        $newPassengerType = $document->createElementNS($airNS, 'air:PassengerType'),
        $passengerType->nextSibling
    );
    // set attributes on the new node
    $newPassengerType->setAttribute('Code', 'ADT');
    $newPassengerType->setAttribute('BookingTravelerRef', 'child_1');
}

$document->formatOutput = TRUE;
echo $document->saveXML();
$document = new DOMDocument();
$document->loadXML($xml_string);
$document->preserveWhiteSpace = FALSE;
$xpath = new DOMXpath($document);

$airNS = "http://www.travelport.com/schema/air_v48_0";
// register the namspace for xpath expressions
$xpath->registerNamespace('air', $airNS);

// fetch matching nodes and delete them
foreach ($xpath->evaluate('//air:BookingInfo[@SegmentRef="82won53R2BKAMqfvFBBB=="]') as $bookingInfo) {
    $bookingInfo->parentNode->removeChild($bookingInfo);
}
// look for the passenger info
foreach ($xpath->evaluate('//air:PassengerType[@Code="CNN"]') as $passengerType) {
    // change the attribute
    $passengerType->setAttribute('BookingTravelerRef', 'child_1');
    // insert the new node after the current one
    $passengerType->parentNode->insertBefore(
        // create a new element node in the namespace
        $newPassengerType = $document->createElementNS($airNS, 'air:PassengerType'),
        $passengerType->nextSibling
    );
    // set attributes on the new node
    $newPassengerType->setAttribute('Code', 'ADT');
    $newPassengerType->setAttribute('BookingTravelerRef', 'child_1');
}

$document->formatOutput = TRUE;
echo $document->saveXML();