我正在尝试解析一个简单的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>
谢谢,非常感谢!
答案 0 :(得分: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;
希望它会有所帮助
答案 1 :(得分:1)
SimpleXML扩展提供了一种获取XML元素名称和文本的简单方法。
与DOM或Expat解析器相比,SimpleXML只需几行代码即可从XML元素中读取文本数据。
SimpleXML将XML文档(或XML字符串)转换为对象,如下所示:
元素将转换为SimpleXMLElement对象的单个属性。当一个级别上有多个元素时,它们将被放置在一个数组中 使用关联数组访问属性,其中索引对应于属性名称 元素内的文本转换为字符串。如果一个元素有多个文本节点,它们将按找到它们的顺序排列 欲了解更多信息,请访 W3Schools
答案 2 :(得分:0)