【问题标题】:XML not correctly converted into objects by simplexml_load_string()simplexml_load_string() 未将 XML 正确转换为对象
【发布时间】:2014-10-26 11:13:01
【问题描述】:

我正在尝试通过 simplexml_load_string() 在 PHP 中解析来自 Amazon API 的 XML 响应。

我得到的 XML 是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<GetMatchingProductForIdResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
   <GetMatchingProductForIdResult Id="xxx" IdType="SellerSKU" status="Success">
      <Products>
         <Product>
            <Identifiers>
               <MarketplaceASIN>
                  <MarketplaceId>xxxx</MarketplaceId>
                  <ASIN>xxx</ASIN>
               </MarketplaceASIN>
            </Identifiers>
            <AttributeSets>
               <ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="de-DE">
                  <ns2:Binding>Elektronik</ns2:Binding>
                  <ns2:Brand>Panasonic</ns2:Brand>
                  <ns2:Feature>xx</ns2:Feature>
                  <ns2:Feature>xxx</ns2:Feature>
                  <ns2:Feature>xxx</ns2:Feature>
                  <ns2:Feature>xxx</ns2:Feature>
                  <ns2:Feature>xxx</ns2:Feature>
                  <ns2:Label>Panasonic</ns2:Label>
                  <ns2:Manufacturer>Panasonic</ns2:Manufacturer>
                  <ns2:PackageDimensions>
                     <ns2:Height Units="inches">xx</ns2:Height>
                     <ns2:Length Units="inches">xx</ns2:Length>
                     <ns2:Width Units="inches">xxx</ns2:Width>
                  </ns2:PackageDimensions>
                  <ns2:PartNumber>xxx</ns2:PartNumber>
                  <ns2:ProductGroup>Computer &amp; Zubehör</ns2:ProductGroup>
                  <ns2:ProductTypeName>COMPUTER_COMPONENT</ns2:ProductTypeName>
                  <ns2:Publisher>Panasonic</ns2:Publisher>
                  <ns2:SmallImage>
                     <ns2:URL>xxx.jpg</ns2:URL>
                     <ns2:Height Units="pixels">xx</ns2:Height>
                     <ns2:Width Units="pixels">xx</ns2:Width>
                  </ns2:SmallImage>
                  <ns2:Studio>Panasonic</ns2:Studio>
                  <ns2:Title>xxx</ns2:Title>
               </ns2:ItemAttributes>
            </AttributeSets>
            <Relationships />
            <SalesRankings />
         </Product>
      </Products>
   </GetMatchingProductForIdResult>
   <ResponseMetadata>
      <RequestId>xx</RequestId>
   </ResponseMetadata>
</GetMatchingProductForIdResponse>    

我的 PHP 代码:

$response = $service->GetMatchingProductForId($request);  //making the call
$response = $response->toXML();
$response = simplexml_load_string($response);       
var_dump($response);     

不幸的是,AttributeSets 并没有像您在 var_dump 中看到的那样转换为对象。

object(SimpleXMLElement)#9 (2) {
  ["GetMatchingProductForIdResult"]=>
  object(SimpleXMLElement)#4 (2) {
    ["@attributes"]=>
    array(3) {
      ["Id"]=>
      string(5) "xxx"
      ["IdType"]=>
      string(9) "SellerSKU"
      ["status"]=>
      string(7) "Success"
    }
    ["Products"]=>
    object(SimpleXMLElement)#14 (1) {
      ["Product"]=>
      object(SimpleXMLElement)#18 (4) {
        ["Identifiers"]=>
        object(SimpleXMLElement)#23 (1) {
          ["MarketplaceASIN"]=>
          object(SimpleXMLElement)#29 (2) {
            ["MarketplaceId"]=>
            string(14) "xxx"
            ["ASIN"]=>
            string(10) "xxxx"
          }
        }
        ["AttributeSets"]=>
        object(SimpleXMLElement)#37 (0) {
        }
        ["Relationships"]=>
        object(SimpleXMLElement)#28 (0) {
        }
        ["SalesRankings"]=>
        object(SimpleXMLElement)#26 (0) {
        }
      }
    }
  }
  ["ResponseMetadata"]=>
  object(SimpleXMLElement)#12 (1) {
    ["RequestId"]=>
    string(36) "xxx"
  }
}

XML 有问题吗?

【问题讨论】:

    标签: php xml parsing object amazon-mws


    【解决方案1】:

    有时 var_dump() 不会显示所有元素(主要是它有太多元素或太多缩进)。它只是关于视图,但你的对象会在那里,尝试只转储 AttributeSets self

    【讨论】:

    • 如果我尝试访问它返回 NULL 的属性,仍然只得到一个空对象object(SimpleXMLElement)#4 (0) { }
    • 您能否提供您的 AttributeSets var_dump 的 php 行。你是如何到达那个元素的?
    • 我认为 products 是一个数组,所以你需要做循环,或者如果它只有一个你可以使用的产品,比如:blabla->products[0]->AttributeSets
    • $response-&gt;GetMatchingProductForIdResult-&gt;Products-&gt;Product-&gt;AttributeSets 返回object(SimpleXMLElement)#4 (0) { },如上所述。也许是因为不同的命名空间?
    • 我刚刚注意到 AttributeSets 的元素具有命名空间前缀,我不太确定,但它可能会引起您的问题 check this issue 似乎 xpath 将是您的解决方案
    【解决方案2】:

    到此为止,只是用了钝力,效果很好。

    $response = str_replace("ns2:","",$response);
    

    【讨论】:

    • 这比我的回答好。
    • 你在哪里给的这条线??请尽快回复
    • 这真是太棒了,它的效果如何。我不知道为什么或如何,或者这是否是最佳实践,但它确实有效,所以我将继续使用它。
    【解决方案3】:

    您可以在您的SimpleXMLElement 上致电children 喜欢

    $xml = <<<EOF
    <AttributeSets>
       <ns2:ItemAttributes xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd" xml:lang="de-DE">
          <ns2:Binding>Elektronik</ns2:Binding>
          <ns2:Brand>Panasonic</ns2:Brand>
          <ns2:Feature>xx</ns2:Feature>
          <ns2:Feature>xxx</ns2:Feature>
          <ns2:Feature>xxx</ns2:Feature>
          <ns2:Feature>xxx</ns2:Feature>
          <ns2:Feature>xxx</ns2:Feature>
          <ns2:Label>Panasonic</ns2:Label>
          <ns2:Manufacturer>Panasonic</ns2:Manufacturer>
          <ns2:PackageDimensions>
             <ns2:Height Units="inches">xx</ns2:Height>
             <ns2:Length Units="inches">xx</ns2:Length>
             <ns2:Width Units="inches">xxx</ns2:Width>
          </ns2:PackageDimensions>
          <ns2:PartNumber>xxx</ns2:PartNumber>
          <ns2:ProductGroup>Computer &amp; Zubehör</ns2:ProductGroup>
          <ns2:ProductTypeName>COMPUTER_COMPONENT</ns2:ProductTypeName>
          <ns2:Publisher>Panasonic</ns2:Publisher>
          <ns2:SmallImage>
             <ns2:URL>xxx.jpg</ns2:URL>
             <ns2:Height Units="pixels">xx</ns2:Height>
             <ns2:Width Units="pixels">xx</ns2:Width>
          </ns2:SmallImage>
          <ns2:Studio>Panasonic</ns2:Studio>
          <ns2:Title>xxx</ns2:Title>
       </ns2:ItemAttributes>
    </AttributeSets>
    EOF;
    
    $sxe = new SimpleXMLElement($xml);
    $ns2 = $sxe->children('ns2', TRUE);
    
    var_dump(count($ns2));
    
    // prints int(1)
    

    在 php 文档中: http://php.net/manual/en/simplexmlelement.children.php

    public SimpleXMLElement SimpleXMLElement::children ([ string $ns [, bool $is_prefix = false ]] )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 1970-01-01
      • 2013-07-13
      • 2012-05-14
      • 2021-10-14
      • 1970-01-01
      • 2012-02-15
      相关资源
      最近更新 更多