您如何解析 MWS GetMatchingProduct 中的关系?
How do you parse Relationships in MWS GetMatchingProduct?
数据:
<Relationships>
<ns2:VariationChild>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
<ASIN>B002KT3XQC</ASIN>
</MarketplaceASIN>
</Identifiers>
<ns2:Color>Black</ns2:Color>
<ns2:Size>Small</ns2:Size>
</ns2:VariationChild>
<ns2:VariationChild>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
<ASIN>B002KT3XQW</ASIN>
</MarketplaceASIN>
</Identifiers>
<ns2:Color>Black</ns2:Color>
<ns2:Size>Medium</ns2:Size>
</ns2:VariationChild>
</Relationships>
代码:
$data = simplexml_load_string($response);
foreach($data->GetMatchingProductResult AS $GetMatchingProductResult){
$Product = $GetMatchingProductResult->Product;
$Relationships = $Product->Relationships;
foreach($Relationships->children('ns2', true)->VariationChild AS $VariationChild){
$Identifiers = $VariationChild->Identifiers;
$MarketplaceASIN = $Identifiers->MarketplaceASIN;
$MarketplaceId = $MarketplaceASIN->MarketplaceId;
$ASIN = $MarketplaceASIN->ASIN;
echo "$ASIN<br />";
}
}
这与 return 相呼应,但没有数据,因此它实际上是在 XML 中循环。然而,我尝试的任何事情实际上都不会 return $ASIN 变量中的数据。这是因为命名空间,还是简单的XML,还是我完全遗漏了其他东西?
编辑:尝试了其他方法
foreach($Relationships->children('http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd', true)->VariationChild AS $VariationChild){
$Identifiers = $VariationChild->Identifiers;
$MarketplaceASIN = $Identifiers->MarketplaceASIN;
$MarketplaceId = $MarketplaceASIN->MarketplaceId;
$ASIN = $MarketplaceASIN->ASIN;
echo "[$ASIN]<br />";
}
$test = new SimpleXMLElement($response);
$test->registerXPathNamespace('ns2', 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd');
$variations = $test->xpath('//ns2:VariationChild');
foreach($variations AS $vars){
print_r($vars);
}
似乎都没有循环数据。
是的,是命名空间用错了。将代码中的 'ns2' 替换为完整的命名空间“http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd”。
imo 更好的解决方案是使用 registerXPathNamespace 然后使用 xpath 访问子元素。
以下代码获取 ASIN
个字符串:
$data = simplexml_load_string($response);
foreach ($data->GetMatchingProductResult as $GetMatchingProductResult) {
$Product = $GetMatchingProductResult->Product;
$Relationships = $Product->Relationships;
foreach ($Relationships->children('ns2', true)->VariationChild
as $VariationChild)
{
foreach ($VariationChild->children('', true) as $var_child) {
echo $var_child->MarketplaceASIN->ASIN, PHP_EOL;
}
}
}
值得一提的是,real response format与您发布的内容不同。
数据:
<Relationships>
<ns2:VariationChild>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
<ASIN>B002KT3XQC</ASIN>
</MarketplaceASIN>
</Identifiers>
<ns2:Color>Black</ns2:Color>
<ns2:Size>Small</ns2:Size>
</ns2:VariationChild>
<ns2:VariationChild>
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
<ASIN>B002KT3XQW</ASIN>
</MarketplaceASIN>
</Identifiers>
<ns2:Color>Black</ns2:Color>
<ns2:Size>Medium</ns2:Size>
</ns2:VariationChild>
</Relationships>
代码:
$data = simplexml_load_string($response);
foreach($data->GetMatchingProductResult AS $GetMatchingProductResult){
$Product = $GetMatchingProductResult->Product;
$Relationships = $Product->Relationships;
foreach($Relationships->children('ns2', true)->VariationChild AS $VariationChild){
$Identifiers = $VariationChild->Identifiers;
$MarketplaceASIN = $Identifiers->MarketplaceASIN;
$MarketplaceId = $MarketplaceASIN->MarketplaceId;
$ASIN = $MarketplaceASIN->ASIN;
echo "$ASIN<br />";
}
}
这与 return 相呼应,但没有数据,因此它实际上是在 XML 中循环。然而,我尝试的任何事情实际上都不会 return $ASIN 变量中的数据。这是因为命名空间,还是简单的XML,还是我完全遗漏了其他东西?
编辑:尝试了其他方法
foreach($Relationships->children('http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd', true)->VariationChild AS $VariationChild){
$Identifiers = $VariationChild->Identifiers;
$MarketplaceASIN = $Identifiers->MarketplaceASIN;
$MarketplaceId = $MarketplaceASIN->MarketplaceId;
$ASIN = $MarketplaceASIN->ASIN;
echo "[$ASIN]<br />";
}
$test = new SimpleXMLElement($response);
$test->registerXPathNamespace('ns2', 'http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd');
$variations = $test->xpath('//ns2:VariationChild');
foreach($variations AS $vars){
print_r($vars);
}
似乎都没有循环数据。
是的,是命名空间用错了。将代码中的 'ns2' 替换为完整的命名空间“http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd”。
imo 更好的解决方案是使用 registerXPathNamespace 然后使用 xpath 访问子元素。
以下代码获取 ASIN
个字符串:
$data = simplexml_load_string($response);
foreach ($data->GetMatchingProductResult as $GetMatchingProductResult) {
$Product = $GetMatchingProductResult->Product;
$Relationships = $Product->Relationships;
foreach ($Relationships->children('ns2', true)->VariationChild
as $VariationChild)
{
foreach ($VariationChild->children('', true) as $var_child) {
echo $var_child->MarketplaceASIN->ASIN, PHP_EOL;
}
}
}
值得一提的是,real response format与您发布的内容不同。