Jump to content

How do I parse diferent info from an XML file when the tags are repeated?


antonb

Recommended Posts

See example below. I would like to be able to do this with SimpleXML...

 

<Options><ServiceDetail><ServiceType>FEDEX_2_DAY_FREIGHT</ServiceType><PackagingType>YOUR_PACKAGING</PackagingType><DeliveryDay>MON</DeliveryDay><DeliveryDate>2008-05-26</DeliveryDate><DestinationStationId>DFW</DestinationStationId><SignatureOption>SERVICE_DEFAULT</SignatureOption></ServiceDetail><ActualRateType>PAYOR_ACCOUNT</ActualRateType><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_ACCOUNT</RateType><RateScale>06704</RateScale><RateZone>5</RateZone><TotalBillingWeight><Units>LB</Units><Value>728.0</Value></TotalBillingWeight><TotalBaseCharge><Currency>USD</Currency><Amount>1142.96</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>228.59</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1371.55</Amount></TotalNetCharge>
<TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_LIST</RateType><RateZone>5</RateZone><TotalBaseCharge><Currency>USD</Currency><Amount>1142.96</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>228.59</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1371.55</Amount></TotalNetCharge><TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails></Options>
<Options><ServiceDetail><ServiceType>FEDEX_1_DAY_FREIGHT</ServiceType><PackagingType>YOUR_PACKAGING</PackagingType><DeliveryDay>FRI</DeliveryDay><DeliveryDate>2008-05-23</DeliveryDate><DestinationStationId>DFW</DestinationStationId><SignatureOption>SERVICE_DEFAULT</SignatureOption></ServiceDetail><ActualRateType>PAYOR_ACCOUNT</ActualRateType><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_ACCOUNT</RateType><RateScale>02176</RateScale><RateZone>5</RateZone><TotalBillingWeight><Units>LB</Units><Value>728.0</Value></TotalBillingWeight><TotalBaseCharge><Currency>USD</Currency><Amount>2278.64</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>455.73</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>2734.37</Amount></TotalNetCharge>
<TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_LIST</RateType><RateZone>5</RateZone><TotalBaseCharge><Currency>USD</Currency><Amount>2278.64</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>455.73</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>2734.37</Amount></TotalNetCharge><TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails></Options>
<Options><ServiceDetail><ServiceType>FEDEX_3_DAY_FREIGHT</ServiceType><PackagingType>YOUR_PACKAGING</PackagingType><DeliveryDay>TUE</DeliveryDay><DeliveryDate>2008-05-27</DeliveryDate><DestinationStationId>DFW</DestinationStationId><SignatureOption>SERVICE_DEFAULT</SignatureOption></ServiceDetail><ActualRateType>PAYOR_ACCOUNT</ActualRateType><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_ACCOUNT</RateType><RateScale>07186</RateScale><RateZone>5</RateZone><TotalBillingWeight><Units>LB</Units><Value>728.0</Value></TotalBillingWeight><TotalBaseCharge><Currency>USD</Currency><Amount>1077.44</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>215.49</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1292.93</Amount></TotalNetCharge>
<TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_LIST</RateType><RateZone>5</RateZone><TotalBaseCharge><Currency>USD</Currency><Amount>1077.44</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>215.49</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1292.93</Amount></TotalNetCharge><TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails></Options>

Link to comment
Share on other sites

First, what you have is not valid XML. There can only be one top level element. But, to fix that, just wrap it in a tag. Here is an example:

 

<?php 
  $xml = '<Options><ServiceDetail><ServiceType>FEDEX_2_DAY_FREIGHT</ServiceType><PackagingType>YOUR_PACKAGING</PackagingType><DeliveryDay>MON</DeliveryDay><DeliveryDate>2008-05-26</DeliveryDate><DestinationStationId>DFW</DestinationStationId><SignatureOption>SERVICE_DEFAULT</SignatureOption></ServiceDetail><ActualRateType>PAYOR_ACCOUNT</ActualRateType><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_ACCOUNT</RateType><RateScale>06704</RateScale><RateZone>5</RateZone><TotalBillingWeight><Units>LB</Units><Value>728.0</Value></TotalBillingWeight><TotalBaseCharge><Currency>USD</Currency><Amount>1142.96</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>228.59</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1371.55</Amount></TotalNetCharge>
<TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_LIST</RateType><RateZone>5</RateZone><TotalBaseCharge><Currency>USD</Currency><Amount>1142.96</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>228.59</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1371.55</Amount></TotalNetCharge><TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails></Options>
<Options><ServiceDetail><ServiceType>FEDEX_1_DAY_FREIGHT</ServiceType><PackagingType>YOUR_PACKAGING</PackagingType><DeliveryDay>FRI</DeliveryDay><DeliveryDate>2008-05-23</DeliveryDate><DestinationStationId>DFW</DestinationStationId><SignatureOption>SERVICE_DEFAULT</SignatureOption></ServiceDetail><ActualRateType>PAYOR_ACCOUNT</ActualRateType><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_ACCOUNT</RateType><RateScale>02176</RateScale><RateZone>5</RateZone><TotalBillingWeight><Units>LB</Units><Value>728.0</Value></TotalBillingWeight><TotalBaseCharge><Currency>USD</Currency><Amount>2278.64</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>455.73</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>2734.37</Amount></TotalNetCharge>
<TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_LIST</RateType><RateZone>5</RateZone><TotalBaseCharge><Currency>USD</Currency><Amount>2278.64</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>455.73</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>2734.37</Amount></TotalNetCharge><TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails></Options>
<Options><ServiceDetail><ServiceType>FEDEX_3_DAY_FREIGHT</ServiceType><PackagingType>YOUR_PACKAGING</PackagingType><DeliveryDay>TUE</DeliveryDay><DeliveryDate>2008-05-27</DeliveryDate><DestinationStationId>DFW</DestinationStationId><SignatureOption>SERVICE_DEFAULT</SignatureOption></ServiceDetail><ActualRateType>PAYOR_ACCOUNT</ActualRateType><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_ACCOUNT</RateType><RateScale>07186</RateScale><RateZone>5</RateZone><TotalBillingWeight><Units>LB</Units><Value>728.0</Value></TotalBillingWeight><TotalBaseCharge><Currency>USD</Currency><Amount>1077.44</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>215.49</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1292.93</Amount></TotalNetCharge>
<TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_LIST</RateType><RateZone>5</RateZone><TotalBaseCharge><Currency>USD</Currency><Amount>1077.44</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>215.49</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1292.93</Amount></TotalNetCharge><TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails></Options>';

  $sxml = simplexml_load_string('<document>'.$xml.'</document>');
  foreach($sxml->Options as $option){
    echo $option->ActualRateType.'<br>';
  }
?>

Link to comment
Share on other sites

Right, but each of the "Options" has two "RatedShipmentDetails", one for PAYOR_ACCOUNT and one for PAYOR_LIST. Each of those has it's own separate TotalNetCharge (even though in your example they are the same amount). What I'm asking is which TotalNetCharge should be compared? The PAYOR_ACCOUNT one or the PAYOR_LIST one?

Link to comment
Share on other sites

Basically, loop over each option, and add it to an array, and it's cost to another array. Then do an array_multisort() and loop over the options array again.

<?php 
  $xml = '<Options><ServiceDetail><ServiceType>FEDEX_2_DAY_FREIGHT</ServiceType><PackagingType>YOUR_PACKAGING</PackagingType><DeliveryDay>MON</DeliveryDay><DeliveryDate>2008-05-26</DeliveryDate><DestinationStationId>DFW</DestinationStationId><SignatureOption>SERVICE_DEFAULT</SignatureOption></ServiceDetail><ActualRateType>PAYOR_ACCOUNT</ActualRateType><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_ACCOUNT</RateType><RateScale>06704</RateScale><RateZone>5</RateZone><TotalBillingWeight><Units>LB</Units><Value>728.0</Value></TotalBillingWeight><TotalBaseCharge><Currency>USD</Currency><Amount>1142.96</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>228.59</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1371.55</Amount></TotalNetCharge>
<TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_LIST</RateType><RateZone>5</RateZone><TotalBaseCharge><Currency>USD</Currency><Amount>1142.96</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>228.59</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1371.55</Amount></TotalNetCharge><TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails></Options>
<Options><ServiceDetail><ServiceType>FEDEX_1_DAY_FREIGHT</ServiceType><PackagingType>YOUR_PACKAGING</PackagingType><DeliveryDay>FRI</DeliveryDay><DeliveryDate>2008-05-23</DeliveryDate><DestinationStationId>DFW</DestinationStationId><SignatureOption>SERVICE_DEFAULT</SignatureOption></ServiceDetail><ActualRateType>PAYOR_ACCOUNT</ActualRateType><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_ACCOUNT</RateType><RateScale>02176</RateScale><RateZone>5</RateZone><TotalBillingWeight><Units>LB</Units><Value>728.0</Value></TotalBillingWeight><TotalBaseCharge><Currency>USD</Currency><Amount>2278.64</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>455.73</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>2734.37</Amount></TotalNetCharge>
<TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_LIST</RateType><RateZone>5</RateZone><TotalBaseCharge><Currency>USD</Currency><Amount>2278.64</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>455.73</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>2734.37</Amount></TotalNetCharge><TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails></Options>
<Options><ServiceDetail><ServiceType>FEDEX_3_DAY_FREIGHT</ServiceType><PackagingType>YOUR_PACKAGING</PackagingType><DeliveryDay>TUE</DeliveryDay><DeliveryDate>2008-05-27</DeliveryDate><DestinationStationId>DFW</DestinationStationId><SignatureOption>SERVICE_DEFAULT</SignatureOption></ServiceDetail><ActualRateType>PAYOR_ACCOUNT</ActualRateType><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_ACCOUNT</RateType><RateScale>07186</RateScale><RateZone>5</RateZone><TotalBillingWeight><Units>LB</Units><Value>728.0</Value></TotalBillingWeight><TotalBaseCharge><Currency>USD</Currency><Amount>1077.44</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>215.49</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1292.93</Amount></TotalNetCharge>
<TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails><RatedShipmentDetails><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount><ShipmentRateDetail><RateType>PAYOR_LIST</RateType><RateZone>5</RateZone><TotalBaseCharge><Currency>USD</Currency><Amount>1077.44</Amount></TotalBaseCharge><TotalFreightDiscounts><Currency>USD</Currency><Amount>0.00</Amount></TotalFreightDiscounts><TotalSurcharges><Currency>USD</Currency><Amount>215.49</Amount></TotalSurcharges><TotalNetCharge><Currency>USD</Currency><Amount>1292.93</Amount></TotalNetCharge><TotalRebates><Currency>USD</Currency><Amount>0.00</Amount></TotalRebates></ShipmentRateDetail><RatedPackages><EffectiveNetDiscount><Currency>USD</Currency><Amount>0.00</Amount></EffectiveNetDiscount></RatedPackages></RatedShipmentDetails></Options>';
  $sxml = simplexml_load_string('<document>'.$xml.'</document>');
  $options = array();
  $cost = array();
  foreach($sxml->Options as $option){
    $options[] = $option;
    $cost[] = (float)$option->RatedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Amount;
  }
  array_multisort($cost,$options);
  foreach($options as $option){
    echo $option->ServiceDetail->ServiceType.'<br>';
  }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.