PHP XML获取节点值

时间:2016-07-27 20:52:35

标签: php xml amazon-mws

我需要能够从这个XML文件中获取信息,以便在我的Magento站点的结账过程中显示在前端。此文件从亚马逊返回给我,并附上我向用户显示的送货信息:

<?xml version="1.0"?>
<GetFulfillmentPreviewResponse xmlns="http://mws.amazonaws.com/FulfillmentOutboundShipment/2010-10-01/">
  <GetFulfillmentPreviewResult>
    <FulfillmentPreviews>
      <member>
        <ShippingSpeedCategory>Standard</ShippingSpeedCategory>
        <IsFulfillable>true</IsFulfillable>
        <IsCODCapable>false</IsCODCapable>
        <EstimatedShippingWeight>
          <Unit>POUNDS</Unit>
          <Value>1</Value>
        </EstimatedShippingWeight>
        <EstimatedFees>
          <member>
            <Name>FBAPerUnitFulfillmentFee</Name>
            <Amount>
              <CurrencyCode>USD</CurrencyCode>
              <Value>0.75</Value>
            </Amount>
          </member>
          <member>
            <Name>FBAPerOrderFulfillmentFee</Name>
            <Amount>
              <CurrencyCode>USD</CurrencyCode>
              <Value>4.75</Value>
            </Amount>
          </member>
          <member>
            <Name>FBATransportationFee</Name>
            <Amount>
              <CurrencyCode>USD</CurrencyCode>
              <Value>0.45</Value>
            </Amount>
          </member>
        </EstimatedFees>
        <FulfillmentPreviewShipments>
          <member>
            <EarliestShipDate>2016-07-28T07:00:00Z</EarliestShipDate>
            <LatestShipDate>2016-07-29T06:59:59Z</LatestShipDate>
            <EarliestArrivalDate>2016-08-02T07:00:00Z</EarliestArrivalDate>
            <LatestArrivalDate>2016-08-05T06:59:59Z</LatestArrivalDate>
            <FulfillmentPreviewItems>
              <member>
                <SellerSKU>BM-1100-14</SellerSKU>
                <Quantity>1</Quantity>
                <SellerFulfillmentOrderItemId>BM-1100-14</SellerFulfillmentOrderItemId>
                <EstimatedShippingWeight>
                  <Unit>POUNDS</Unit>
                  <Value>0.119</Value>
                </EstimatedShippingWeight>
                <ShippingWeightCalculationMethod>Dimensional</ShippingWeightCalculationMethod>
              </member>
            </FulfillmentPreviewItems>
          </member>
        </FulfillmentPreviewShipments>
        <UnfulfillablePreviewItems/>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
      </member>
      <member>
        <ShippingSpeedCategory>Priority</ShippingSpeedCategory>
        <IsFulfillable>true</IsFulfillable>
        <IsCODCapable>false</IsCODCapable>
        <EstimatedShippingWeight>
          <Unit>POUNDS</Unit>
          <Value>1</Value>
        </EstimatedShippingWeight>
        <EstimatedFees>
          <member>
            <Name>FBAPerUnitFulfillmentFee</Name>
            <Amount>
              <CurrencyCode>USD</CurrencyCode>
              <Value>0.75</Value>
            </Amount>
          </member>
          <member>
            <Name>FBAPerOrderFulfillmentFee</Name>
            <Amount>
              <CurrencyCode>USD</CurrencyCode>
              <Value>14.75</Value>
            </Amount>
          </member>
          <member>
            <Name>FBATransportationFee</Name>
            <Amount>
              <CurrencyCode>USD</CurrencyCode>
              <Value>1.50</Value>
            </Amount>
          </member>
        </EstimatedFees>
        <FulfillmentPreviewShipments>
          <member>
            <EarliestShipDate>2016-07-29T00:00:00Z</EarliestShipDate>
            <LatestShipDate>2016-07-29T00:00:00Z</LatestShipDate>
            <EarliestArrivalDate>2016-07-29T07:00:00Z</EarliestArrivalDate>
            <LatestArrivalDate>2016-07-30T06:59:59Z</LatestArrivalDate>
            <FulfillmentPreviewItems>
              <member>
                <SellerSKU>BM-1100-14</SellerSKU>
                <Quantity>1</Quantity>
                <SellerFulfillmentOrderItemId>BM-1100-14</SellerFulfillmentOrderItemId>
                <EstimatedShippingWeight>
                  <Unit>POUNDS</Unit>
                  <Value>0.119</Value>
                </EstimatedShippingWeight>
                <ShippingWeightCalculationMethod>Dimensional</ShippingWeightCalculationMethod>
              </member>
            </FulfillmentPreviewItems>
          </member>
        </FulfillmentPreviewShipments>
        <UnfulfillablePreviewItems/>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
      </member>
      <member>
        <ShippingSpeedCategory>Expedited</ShippingSpeedCategory>
        <IsFulfillable>true</IsFulfillable>
        <IsCODCapable>false</IsCODCapable>
        <EstimatedShippingWeight>
          <Unit>POUNDS</Unit>
          <Value>1</Value>
        </EstimatedShippingWeight>
        <EstimatedFees>
          <member>
            <Name>FBAPerUnitFulfillmentFee</Name>
            <Amount>
              <CurrencyCode>USD</CurrencyCode>
              <Value>0.75</Value>
            </Amount>
          </member>
          <member>
            <Name>FBAPerOrderFulfillmentFee</Name>
            <Amount>
              <CurrencyCode>USD</CurrencyCode>
              <Value>7.75</Value>
            </Amount>
          </member>
          <member>
            <Name>FBATransportationFee</Name>
            <Amount>
              <CurrencyCode>USD</CurrencyCode>
              <Value>0.55</Value>
            </Amount>
          </member>
        </EstimatedFees>
        <FulfillmentPreviewShipments>
          <member>
            <EarliestShipDate>2016-07-29T00:00:00Z</EarliestShipDate>
            <LatestShipDate>2016-07-29T00:00:00Z</LatestShipDate>
            <EarliestArrivalDate>2016-08-01T07:00:00Z</EarliestArrivalDate>
            <LatestArrivalDate>2016-08-02T06:59:59Z</LatestArrivalDate>
            <FulfillmentPreviewItems>
              <member>
                <SellerSKU>BM-1100-14</SellerSKU>
                <Quantity>1</Quantity>
                <SellerFulfillmentOrderItemId>BM-1100-14</SellerFulfillmentOrderItemId>
                <EstimatedShippingWeight>
                  <Unit>POUNDS</Unit>
                  <Value>0.119</Value>
                </EstimatedShippingWeight>
                <ShippingWeightCalculationMethod>Dimensional</ShippingWeightCalculationMethod>
              </member>
            </FulfillmentPreviewItems>
          </member>
        </FulfillmentPreviewShipments>
        <UnfulfillablePreviewItems/>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
      </member>
    </FulfillmentPreviews>
  </GetFulfillmentPreviewResult>
  <ResponseMetadata>
    <RequestId>eced620f-543d-4d45-9262-1ac49a638d37</RequestId>
  </ResponseMetadata>
</GetFulfillmentPreviewResponse>

如何正确阅读本文档并获取所需信息?

1 个答案:

答案 0 :(得分:0)

See this answer for a similar question.

//convert an xml string into an array
function xml2array($xml){
    $xml = preg_replace('/(<\/?)\w+:([^>]*>)/', '$1$2', $xml); //get rid of namespaces
    $xml = simplexml_load_string($xml);
    return json_decode(json_encode($xml),true); //use built-in stuff
}

然后

$array = xml2array($your_xml_text);
print_r($array);

将xml解析为数组后,您可以轻松获取所需的数据。