簡體   English   中英

如何從php中的xml檢索值?

[英]how can i retrieve values from xml in php?

我正在嘗試解析一個簡單的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>

謝謝,幫助表示贊賞!

嘗試這個 :

$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;

希望對你有幫助

SimpleXML擴展提供了一種獲取XML元素的名稱和文本的簡單方法。

與DOM或Expat解析器相比,SimpleXML僅需要幾行代碼即可從XML元素讀取文本數據。

SimpleXML將XML文檔(或XML字符串)轉換為對象,如下所示:

元素被轉換為SimpleXMLElement對象的單個屬性。 如果在一個級別上有多個元素,則將它們放置在數組內。使用關聯數組訪問屬性,其中索引對應於屬性名稱,元素內的文本轉換為字符串。 如果元素具有多個文本節點,則將按照找到的順序排列它們。有關更多信息,請訪問: W3Schools

我使用過對我來說很好用的功能。

你嘗試過這個嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM