在先前的教程您可能已閱讀, XML解析使用PHP ) (中間,充分XML解析器采用一流的,這需要噸行的代碼和額外的時間。在PHP5 ,我們可以使用簡單的XML類快速解析XML在只有兩行代碼。
我們的可擴展標記語言文字
我使用相同的文字作為Jubba的XML解析教程,但我可以補充一些屬性向您展示的額外權力的簡單的XML :
<description>Equipped with a Japanese Mind-control device, the giant monster has attacked important harbours along the California coast. President to take action. </description>
</story>
<story>
<headline> Bigfoot Spotted at M.I.T. Dining Area </headline>
<description>The beast was seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported missing. </description>
</story>
<story>
<headline> London Angel Saves England </headline>
<description>The "London Angel" known only as "Kit" has saved the U.K. yet again. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled thank you goes out to her. </description>
</story>
<story>
<headline> Six-eyed Man to be Wed to an Eight-armed Woman </headline>
<description>Uhhhmmm... No comment really... just a little creepy to see them together... </description>
</story>
<story>
<headline> Ahmed's Birthday Extravaganza! </headline>
<description>The gifted youngster's birthday party should be a blast. He is turning thirteen and has requested a large cake, ice cream, and a petting zoo complete with pony rides. </description>
</story>
</news>
我們的PHP代碼
我知道你預期兩百名左右線的先進PHP和無法理解的評論。你錯了,這裡的兩行你需要一個簡單的XML對象:
//Since we're already using PHP5, why don't we exploit their easy to use file_get_contents() command?
$xmlFileData = file_get_contents(“input.xml”);
//Here's our Simple XML parser!
$xmlData = new SimpleXMLElement($xmlFileData);
//And here's the output.
print_r($xmlData);
執行,在你的PHP服務器,你得到一些毫無意義的亂碼像以下。
SimpleXMLElement Object (
[story] => Array (
[0] => SimpleXMLElement Object (
[headline] => Godzilla Attacks LA!
[description] => Equipped with a Japanese Mind-control device, the giant monster has attacked important harbours along the California coast. President to take action.
)
[1] => SimpleXMLElement Object (
[headline] => Bigfoot Spotted at M.I.T. Dining Area
[description] => The beast was seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported missing.
)
[2] => SimpleXMLElement Object (
[headline] => London Angel Saves England
[description] => The "London Angel" known only as "Kit" has saved the U.K. yet again. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled thank you goes out to her.
)
[3] => SimpleXMLElement Object (
[headline] => Six-eyed Man to be Wed to an Eight-armed Woman
[description] => Uhhhmmm... No comment really... just a little creepy to see them together...
)
[4] => SimpleXMLElement Object (
[headline] => Ahmed's Birthday Extravaganza!
[description] => The gifted youngster's birthday party should be a blast. He is turning thirteen and has requested a large cake, ice cream, and a petting zoo complete with pony rides.
)
)
)
)
為了獲取數據實際上從混亂的價值觀,我們可以使用它作為數組一類適用於它。像這樣:
//Retrieving the headline from the first story
$xmlHeadline = $xmlData->story[0]->headline;
//Printing our first headline
print($xmlHeadline);
但是,如果你希望的日期的故事,但並不想加入另一個節點陣列的故事?只需添加一個屬性。和簡單的XML也能處理的屬性!因此,我們新的XML看起來是這樣的:
<?xml version="1.0"?>
<news>
<story>
<headline date=”January 19, 2005”> Godzilla Attacks LA! </headline>
<description>Equipped with a Japanese Mind-control device, the giant monster has attacked important harbours along the California coast. President to take action. </description>
</story>
<story>
<headline date=”February 14, 2006”> Bigfoot Spotted at M.I.T. Dining Area </headline>
<description>The beast was seen ordering a Snapple in the dining area on Tuesday. In a related story, Kirupa Chinnathambi, an MIT engineering student has been reported missing. </description>
</story>
<story>
<headline date=”May 27, 2006”> London Angel Saves England </headline>
<description>The "London Angel" known only as "Kit" has saved the U.K. yet again. Reports have stated that she destroyed every single Churchill bobble-head dog in the country. A great heartfilled thank you goes out to her. </description>
</story>
<story>
<headline date=”June 3, 2006”> Six-eyed Man to be Wed to an Eight-armed Woman </headline>
<description>Uhhhmmm... No comment really... just a little creepy to see them together... </description>
</story>
<story>
<headline date=”July 28, 2006”> Ahmed's Birthday Extravaganza! </headline>
<description>The gifted youngster's birthday party should be a blast. He is turning thirteen and has requested a large cake, ice cream, and a petting zoo complete with pony rides. </description>
</story>
</news>
所以,現在我們有我們的所有新聞和日期,但我們如何顯示它的人?我們可以使用一個簡單的foreach循環輸出所有的人
//Outputing all of our XML to people
foreach($xmlData->story as $story) {
print(“<h2>” . $story->headline . “</h2><br />”);
print($story->description . “<br />_________________________<br />”);
print($story->headline["date"] . “<br /><br />”);
}