-1

我正在尝试解析一个简单的 xml 字符串并存储到数组中。

我希望将信息放入数组中,以便我可以以某种方式从索引中获取它,有人可以帮助我吗?

这是我目前正在尝试的:

<?php

$string = file_get_contents("http://api.themoviedb.org/2.1/Person.search/en/xml/e72f8f2f685df4dad86f939097d14f36/Brad+Pitt");
$xml = new SimpleXMLElement($string); 


foreach($xml->children()->children()->children() as $child)
{
    if ($child->getName() == "images")
        echo $child[0];
}

$error_code = (string)$body[0]->Response->return->error_code;
print_r($error_code); 
?> 

这是 xml 响应:

<OpenSearchDescription><opensearch:Query searchTerms="brad+pitt"/><opensearch:totalResults>1</opensearch:totalResults><people><person><score>1</score><popularity>3</popularity><name>Brad Pitt</name><id>287</id><biography>From Wikipedia, the free encyclopedia. William Bradley "Brad" Pitt (born December 18, 1963) is an American actor and film producer. Pitt has received two Academy Award nominations and four Golden Globe Award nominations, winning one. He has been described as one of the world's most attractive men, a label for which he has received substantial media attention. Pitt began his acting career with television guest appearances, including a role on the CBS prime-time soap opera Dallas in 1987. He later gained recognition as the cowboy hitchhiker who seduces Geena Davis's character in the 1991 road movie Thelma & Louise. Pitt's first leading roles in big-budget productions came with A River Runs Through It (1992) and Interview with the Vampire (1994). He was cast opposite Anthony Hopkins in the 1994 drama Legends of the Fall, which earned him his first Golden Globe nomination. In 1995 he gave critically acclaimed performances in the crime thriller Seven and the science fiction film 12 Monkeys, the latter securing him a Golden Globe Award for Best Supporting Actor and an Academy Award nomination. Four years later, in 1999, Pitt starred in the cult hit Fight Club. He then starred in the major international hit as Rusty Ryan in Ocean's Eleven (2001) and its sequels, Ocean's Twelve (2004) and Ocean's Thirteen (2007). His greatest commercial successes have been Troy (2004) and Mr. & Mrs. Smith (2005). Pitt received his second Academy Award nomination for his title role performance in the 2008 film The Curious Case of Benjamin Button. Following a high-profile relationship with actress Gwyneth Paltrow, Pitt was married to actress Jennifer Aniston for five years. Pitt lives with actress Angelina Jolie in a relationship that has generated wide publicity. He and Jolie have six children—Maddox, Pax, Zahara, Shiloh, Knox, and Vivienne. Since beginning his relationship with Jolie, he has become increasingly involved in social issues both in the United States and internationally. Pitt owns a production company named Plan B Entertainment, whose productions include the 2007 Academy Award winning Best Picture, The Departed. Description above from the Wikipedia article Brad Pitt, licensed under CC-BY-SA, full list of contributors on Wikipedia.</biography><url>http://www.themoviedb.org/person/287</url><images><image type="profile" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w45/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg" size="thumb" width="45" height="68" id="4ea5cb8c2c0588394800006f"/><image type="profile" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg" size="profile" width="185" height="281" id="4ea5cb8c2c0588394800006f"/><image type="profile" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/h632/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg" size="h632" width="416" height="632" id="4ea5cb8c2c0588394800006f"/><image type="profile" url="http://d3gtl9l2a4fn1j.cloudfront.net/t/p/original/w8zJQuN7tzlm6FY9mfGKihxp3Cb.jpg" size="original" width="1295" height="1969" id="4ea5cb8c2c0588394800006f"/></images><version>685</version><last_modified_at>2013-07-26 18:18:17 UTC</last_modified_at></person></people></OpenSearchDescription>

谢谢,帮助表示赞赏!

4

3 回答 3

2

尝试这个 :

$string = file_get_contents("http://api.themoviedb.org/2.1/Person.search/en/xml/e72f8f2f685df4dad86f939097d14f36/Brad+Pitt");
$xml = new SimpleXMLElement($string); 


foreach($xml->people->person->images->image as $child){
    $node = (array)$child;
    print_r($node);
    echo $node['@attributes']['url'];
}

echo "<pre>";
print_r($xml->people->person->images->image); 
exit;

希望它会有所帮助

于 2013-08-01T04:24:48.893 回答
1

SimpleXML 扩展提供了一种获取 XML 元素名称和文本的简单方法。

与 DOM 或 Expat 解析器相比,SimpleXML 只需几行代码即可从 XML 元素中读取文本数据。

SimpleXML 将 XML 文档(或 XML 字符串)转换为对象,如下所示:

元素被转换为 SimpleXMLElement 对象的单个属性。当一个级别上有多个元素时,它们被放置在一个数组中 使用关联数组访问属性,其中索引对应于属性名称 元素内部的文本被转换为字符串。如果一个元素有多个文本节点,它们将按照它们被发现的顺序排列。更多信息请访问: W3Schools

于 2013-08-01T04:22:12.773 回答
0

我已经使用了对我来说很好的功能。

你试过这个吗?

http://www.bin-co.com/php/scripts/xml2array/

于 2013-08-01T05:07:31.423 回答