0

当我尝试从 XML 文件中提取信息时出现此错误。获取信息的代码是:

//retrieve amazon data
$parsed_xml = amazon_xml($isbn);
$amazonResult = array();

    //print_r($parsed_xml); die;
$current = $parsed_xml->ListMatchingProductsResponse->ListMatchingProductsResult->Products->Product;
    //if($parsed_xml) {

        $amazonResult = array(
            'Title' => $current->AttributeSets->children('ns2')->ItemAttributes->Title,
            'SalesRank' => $current->SalesRankings->SalesRank->Rank,
            'ListPrice' => $current->AttributeSets->ItemAttributes->ListPrice->Amount,
            'ImageURL' => $current->AttributeSets->ItemAttributes->SmallImage->URL,
            'DetailURL' => $current->AttributeSets->ItemAttributes->SmallImage->URL,

XML文件是:

<?xml version="1.0"?>
<ListMatchingProductsResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<ListMatchingProductsResult>
<Products xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
<Product>
<AttributeSets>
<ns2:ItemAttributes xml:lang="en-US">
<ns2:Title>JavaScript: The Missing Manual</ns2:Title>

当我运行它时,我得到一个空值,当我检查错误日志时,我得到:Call to a member function children() on a non-object in ... 在第 52 行。违规行是:

'Title' => $current->AttributeSets->children('ns2')->ItemAttributes->Title,

唯一具有 ns2 命名空间的项是 ItemAttributes 和 Title。

如何更正此问题以便获取标题信息?谢谢

4

1 回答 1

1

ns2是前缀,不是实际的命名空间,ListMatchingProductsResponse是根节点,不用提了,所以:

var_dump(
        $parsed_xml->ListMatchingProductsResult
                ->Products
                ->Product
                ->AttributeSets
                ->children('ns2',true)
                ->ItemAttributes
                ->Title);              
于 2012-07-11T19:31:10.117 回答