xml标记中的冒号无法在php

时间:2015-12-29 06:56:33

标签: php xml

我需要一些东西来代替冒号,或找到一种方法来处理它。此标记位于多个其他标记内。这个XML就是问题所在:

<ns2:Amount>9.99</ns2:Amount>

这是完整的xml

 <GetMatchingProductForIdResult Id="082686322638" IdType="UPC" status="Success">
<Products>
  <Product>
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
        <ASIN>B00TU53O8Q</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <AttributeSets>
      <ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="en-US">
        <ns2:Binding>Toy</ns2:Binding>
        <ns2:Brand>Rubie's</ns2:Brand>
        <ns2:Color>BLACK</ns2:Color>
        <ns2:Department>unisex-child</ns2:Department>
        <ns2:Feature>Half Helmet mask accessory covers face; one size fits most</ns2:Feature>
        <ns2:Feature>Officially licensed Star Wars Episode VII costume accessory; as worn by Kylo Ren in Star Wars Episode VII: The Force Awakens</ns2:Feature>
        <ns2:Feature>Helmet has faux battle damage representing a pivotal moment in the movie</ns2:Feature>
        <ns2:Feature>Kylo Ren costumes and additional accessories available separately</ns2:Feature>
        <ns2:Feature>Rubie's offers costumes and accessories from Star Wars original and prequel trilogies as well as the Clone Wars series; great group and family costume ideas</ns2:Feature>
        <ns2:ItemDimensions>
          <ns2:Height Units="inches">10.00</ns2:Height>
          <ns2:Length Units="inches">10.00</ns2:Length>
          <ns2:Width Units="inches">10.00</ns2:Width>
          <ns2:Weight Units="pounds">0.13</ns2:Weight>
        </ns2:ItemDimensions>
        <ns2:IsAdultProduct>false</ns2:IsAdultProduct>
        <ns2:Label>Rubies - Domestic</ns2:Label>
        <ns2:ListPrice>
          <ns2:Amount>9.99</ns2:Amount>
          <ns2:CurrencyCode>USD</ns2:CurrencyCode>
        </ns2:ListPrice>
        <ns2:Manufacturer>Rubies - Domestic</ns2:Manufacturer>
        <ns2:ManufacturerMaximumAge Units="months">120.0</ns2:ManufacturerMaximumAge>
        <ns2:ManufacturerMinimumAge Units="months">60.0</ns2:ManufacturerMinimumAge>
        <ns2:Model>32263</ns2:Model>
        <ns2:NumberOfItems>1</ns2:NumberOfItems>
        <ns2:PackageDimensions>
          <ns2:Height Units="inches">4.00</ns2:Height>
          <ns2:Length Units="inches">9.20</ns2:Length>
          <ns2:Width Units="inches">8.20</ns2:Width>
          <ns2:Weight Units="pounds">0.15</ns2:Weight>
        </ns2:PackageDimensions>
        <ns2:PackageQuantity>1</ns2:PackageQuantity>
        <ns2:PartNumber>32263</ns2:PartNumber>
        <ns2:ProductGroup>Toy</ns2:ProductGroup>
        <ns2:ProductTypeName>TOYS_AND_GAMES</ns2:ProductTypeName>
        <ns2:Publisher>Rubies - Domestic</ns2:Publisher>
        <ns2:Size>One Size</ns2:Size>
        <ns2:SmallImage>
          <ns2:URL>http://ecx.images-amazon.com/images/I/41XuPVJMMkL._SL75_.jpg</ns2:URL>
          <ns2:Height Units="pixels">75</ns2:Height>
          <ns2:Width Units="pixels">66</ns2:Width>
        </ns2:SmallImage>
        <ns2:Studio>Rubies - Domestic</ns2:Studio>
        <ns2:Title>Star Wars: The Force Awakens Child's Kylo Ren Half Helmet</ns2:Title>
        <ns2:Warranty>No Warranty</ns2:Warranty>
      </ns2:ItemAttributes>
    </AttributeSets>
    <Relationships/>
    <SalesRankings>
      <SalesRank>
        <ProductCategoryId>toy_display_on_website</ProductCategoryId>
        <Rank>5183</Rank>
      </SalesRank>
      <SalesRank>
        <ProductCategoryId>2229575011</ProductCategoryId>
        <Rank>11</Rank>
      </SalesRank>
    </SalesRankings>
  </Product>
</Products>
</GetMatchingProductForIdResult>

我需要在PHP中使用amount = 9.99

2 个答案:

答案 0 :(得分:1)

尝试使用基本的PHP:

$subject = "<ns2:Amount>9.99</ns2:Amount>
<ns2:Test>ABCD</ns2:Test>"; // suppose you have two tags
preg_match_all('/<[^:]*:([^>]*)>(.*)<\/[^>]*>/', $subject, $matches); // matching

$i = 1;
$ii = 0;
foreach ($matches as $key => $value) {          
    $newArr[$matches[$i][$ii]] = $matches[$ii][$ii];            
    $ii++;
    $i++;
}

$result = array_filter($newArr); 
foreach($result as $key => $value){
  echo $key." = ".$value. "<br/>";
}

结果是:

Amount = 9.99
Test = ABCD

答案 1 :(得分:0)

我能够创建一个让我忽略名称空间的代码。这就是它的样子。

$Amount = $xml->GetMatchingProductForIdResult[0]->Products->Product->AttributeSets->children('ns2', true)->ItemAttributes->Binding->Amount;