Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对非对象调用成员函数children()_Php_Xml_Parsing_Simplexml - Fatal编程技术网

Php 对非对象调用成员函数children()

Php 对非对象调用成员函数children(),php,xml,parsing,simplexml,Php,Xml,Parsing,Simplexml,当我试图从XML文件中提取信息时,我遇到了这个错误。获取信息的代码是: //retrieve amazon data $parsed_xml = amazon_xml($isbn); $amazonResult = array(); //print_r($parsed_xml); die; $current = $parsed_xml->ListMatchingProductsResponse->ListMatchingProductsResult->Products

当我试图从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>
名称空间为ns2的唯一项是ItemAttributes和Title

如何更正此错误,以便获取标题信息?
感谢

ns2
是一个前缀,而不是实际的名称空间,
ListMatchingProductsResponse
是根节点,无需提及,因此:

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

$current是前4个项目的值,这样我就不必为数组中的其他项目一直写入它。我应该将它添加到哪里?在我的阵法之前还是之后?嗯。。。从你喜欢的任何一点开始?我怀疑你想要一个
var\u dump
,这只是为了说明事情是如何发生的。。。第一件事是取出
->ListMatchingProductsResponse
,第二件事是在
子项('ns2')
中添加
,true
…好的,我做了所有这些,但我仍然在服务器上收到错误消息。还有什么我可以试试的吗?当我打印时($parsed_xml);死亡我看到所有的信息都在那里,我只是不能把它放在我的数组中。它在这里工作(tm),你能指出吗?我在第18行注意到你有:$parsed_xml=new simplexmlement($xml);我的$parsed_xml=amazon_xml($isbn);isbn通过另一个文件输入,amazon_xml是返回信息的函数。我应该读:$parsed_xml=newsimplexmlement(amazon_xml($isbn))?
var_dump(
        $parsed_xml->ListMatchingProductsResult
                ->Products
                ->Product
                ->AttributeSets
                ->children('ns2',true)
                ->ItemAttributes
                ->Title);