问题描述:

I have XML of the form

$xml=<<<EOD

<?xml version="1.0" encoding="utf-8"?>

<CompleteSaleRequest xmlns="urn:ebay:apis:eBLBaseComponents">

<RequesterCredentials>

<eBayAuthToken>AUTH_TOKEN_VALUE</eBayAuthToken>

</RequesterCredentials>

<ItemID>ItemIDVALUE</ItemID>

<TransactionID>TransactionIDVALUE</TransactionID>

<Shipped>1</Shipped>

<Paid>1</Paid>

<Shipment>

<ShipmentTrackingDetails>

<ShipmentTrackingNumber>$trackingNo</ShipmentTrackingNumber>

<ShippingCarrierUsed>UPS</ShippingCarrierUsed>

</ShipmentTrackingDetails>

<ShippedTime>2014-09-30 12:41:59.202303</ShippedTime>

</Shipment>

<ErrorLanguage> string </ErrorLanguage>

EOD;

I want to append more ShipmentTrackingDetails portion using PHP, I need the resulting XML like this:

$xml=<<<EOD

<?xml version="1.0" encoding="utf-8"?>

<CompleteSaleRequest xmlns="urn:ebay:apis:eBLBaseComponents">

<RequesterCredentials>

<eBayAuthToken>AUTH_TOKEN_VALUE</eBayAuthToken>

</RequesterCredentials>

<ItemID>ItemIDVALUE</ItemID>

<TransactionID>TransactionIDVALUE</TransactionID>

<Shipped>1</Shipped>

<Paid>1</Paid>

<Shipment>

<ShipmentTrackingDetails>

<ShipmentTrackingNumber>$trackingNo</ShipmentTrackingNumber>

<ShippingCarrierUsed>UPS</ShippingCarrierUsed>

</ShipmentTrackingDetails>

<ShipmentTrackingDetails>

<ShipmentTrackingNumber>$trackingNo2</ShipmentTrackingNumber>

<ShippingCarrierUsed>UPS</ShippingCarrierUsed>

</ShipmentTrackingDetails>

<ShipmentTrackingDetails>

<ShipmentTrackingNumber>$trackingNo3</ShipmentTrackingNumber>

<ShippingCarrierUsed>UPS</ShippingCarrierUsed>

</ShipmentTrackingDetails>

<ShippedTime>2014-09-30 12:41:59.202303</ShippedTime>

</Shipment>

<ErrorLanguage> string </ErrorLanguage>

EOD;

PHP Source:

$sxe = new SimpleXMLElement($xml);

$track = $sxe->addChild('ShipmentTrackingDetails');

$track->addChild("ShipmentTrackingNumber", "9876");

$track->addChild("ShippingCarrierUsed", "USPS");

I am not getting how to do this using php.

Please help in getting this working.

Thanks!

网友答案:

I got it, The dynamic part would be:

 $sxe = new SimpleXMLElement($xml);  
    $TrackingDetails = $sxe->Shipment->addChild('ShipmentTrackingDetails'); 
    $TrackingDetails->addChild("ShipmentTrackingNumber", $trackingMore); 
    $TrackingDetails->addChild("ShippingCarrierUsed", "UPS"); 
    $sxe->asXML("tracking.xml");
    $xml1 = $sxe->saveXML();

Where $xml is the original xml to which we want to add new child.

相关阅读:
Top