我试图通过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 & 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);
遗憾的是,如var_dump中所见,AttributeSets无法转换为对象。
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有问题吗?
答案 0 :(得分:5)
坚持这一点,只是使用钝力而且效果很好。
$response = str_replace("ns2:","",$response);
答案 1 :(得分:0)
有时var_dump()不会显示所有元素(大多数元素太多或缩进太多)。它只关于视图,但你的对象将在那里,尝试只转储AttributeSets self
答案 2 :(得分:0)
您可以在children
上致电SimpleXMLElement
$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 & 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 ]] )