Jim
Jim

Reputation: 1315

Call to a member function children() on a non-object

I am getting this error when I try to extract information from an XML file. The code to get the information is:

//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,

and the XML file is:

<?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>

When i run this I get a null value, and when I check the error log I get: Call to a member function children() on a non-object in ... on line 52. The offending line is:

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

The only items that have the namespace of ns2 are ItemAttributes and Title.

How do I correct this so I can get the title information? Thanks

Upvotes: 0

Views: 2986

Answers (1)

Wrikken
Wrikken

Reputation: 70500

ns2 is a prefix, not the actual namespace,and ListMatchingProductsResponse is the root-node, no need to mention it, so:

var_dump(
        $parsed_xml->ListMatchingProductsResult
                ->Products
                ->Product
                ->AttributeSets
                ->children('ns2',true)
                ->ItemAttributes
                ->Title);              

Upvotes: 1

Related Questions