2011-12-21 99 views
0

我有一些電影中的數據的XML,我想用所有電影製作一個XML ..我該怎麼做?將XML節點複製到XML節點集合(PHP)

這是電影XML的例子:

<movie> 
<popularity>0.018</popularity> 
<translated>true</translated> 
<adult>false</adult> 
<language>en</language> 
<original_name>Return to Rajapur</original_name> 
<name>Return to Rajapur</name> 
<alternative_name>Retorno a Rajapur</alternative_name> 
<type>movie</type> 
<id>80261</id> 
<imdb_id>tt0444415</imdb_id> 
<url>http://www.themoviedb.org/movie/80261</url> 
<overview> 
A doomed love affair blooms against the beautiful and exotic backdrop of the deserts of India in this romantic drama. Samantha Hartley (Kelli Garner) is a woman in her early twenties who travels to Rajapur in India to visit a resort where her mother stayed years ago. While tracing the steps of her mother, Sara (Lynn Collins), Samantha learns the true story about her mother's stormy marriage to Jeremy (Justin Theroux), a charming but moody alcoholic. Only a few days after their wedding, Sara began to wonder if marrying Jeremy was a mistake, and while visiting India on their honeymoon, Sara met Jai Singh (Manoj Bajpai), a handsome and sensitive widower living in Rajapur. Jai Singh, who speaks fluent English, soon strikes up a friendship with Sara that quickly grows into a romance, but both are aware of the transgressive nature of their love, and their affair takes a tragic turn, leaving its scars on all parties involved. 
</overview> 
<votes>0</votes> 
<rating>0.0</rating> 
<tagline/> 
<certification/> 
<released>2006-11-01</released> 
<runtime>97</runtime> 
<budget/> 
<revenue/> 
<homepage/> 
<trailer>http://www.youtube.com/watch?v=b2bc8dHUco4</trailer> 
<categories></categories> 
<keywords></keywords> 
<studios></studios> 
<languages_spoken> 
<language_spoken code="en" name="English" native_name="English"/> 
<language_spoken code="hi" name="Hindi" native_name=""/> 
</languages_spoken> 
<countries></countries> 
<images> 
<image type="poster" url="http://cf2.imgobject.com/t/p/w92/tPuPbPCg0cAwWAAasVAyy4366BA.jpg" size="thumb" width="92" height="136" id="4ee0cac57b9aa14e0f00272e"/> 
<image type="poster" url="http://cf2.imgobject.com/t/p/w154/tPuPbPCg0cAwWAAasVAyy4366BA.jpg" size="w154" width="154" height="228" id="4ee0cac57b9aa14e0f00272e"/> 
<image type="poster" url="http://cf2.imgobject.com/t/p/w185/tPuPbPCg0cAwWAAasVAyy4366BA.jpg" size="cover" width="185" height="274" id="4ee0cac57b9aa14e0f00272e"/> 
<image type="poster" url="http://cf2.imgobject.com/t/p/w342/tPuPbPCg0cAwWAAasVAyy4366BA.jpg" size="w342" width="342" height="507" id="4ee0cac57b9aa14e0f00272e"/> 
<image type="poster" url="http://cf2.imgobject.com/t/p/w500/tPuPbPCg0cAwWAAasVAyy4366BA.jpg" size="mid" width="500" height="741" id="4ee0cac57b9aa14e0f00272e"/> 
<image type="poster" url="http://cf2.imgobject.com/t/p/original/tPuPbPCg0cAwWAAasVAyy4366BA.jpg" size="original" width="1120" height="1660" id="4ee0cac57b9aa14e0f00272e"/> 
</images> 
<cast> 
<person name="Nanda Anand" character="" job="Director" id="589088" thumb="" department="Directing" url="http://www.themoviedb.org/person/589088" order="0" cast_id="1000"/> 
<person name="Lynn Collins" character="" job="Actor" id="21044" thumb="http://cf2.imgobject.com/t/p/w185/berS11tKvXqTFThUWAYrH279cvn.jpg" department="Actors" url="http://www.themoviedb.org/person/21044" order="0" cast_id="1001"/> 
<person name="Kelli Garner" character="" job="Actor" id="17442" thumb="http://cf2.imgobject.com/t/p/w185/tV4nlT3ApNd2iQMN2JGr6uCnoxX.jpg" department="Actors" url="http://www.themoviedb.org/person/17442" order="1" cast_id="1002"/> 
<person name="Justin Theroux" character="" job="Actor" id="15009" thumb="http://cf2.imgobject.com/t/p/w185/hkJ8UaqMacoIiVkqu1AG69cIjMI.jpg" department="Actors" url="http://www.themoviedb.org/person/15009" order="2" cast_id="1003"/> 
</cast> 
<version>1</version> 
<last_modified_at>2011-12-21 11:18:06 UTC</last_modified_at> 
</movie> 

我需要一個XML是這樣的:

<movies> 
<movie> 
</movie> 
</movies> 

我敢肯定有辦法,我可以做到這一點,但我關於PHP和XML的新內容,所以我需要幫助! 謝謝!

回答

1

這不是一個非常優雅的解決方案,但它可以工作:可以讀取每個文件,並把他們的內容在一個文件中:

<?php 
$files = array("file1.xml","file2.xml","file3.xml"); 
$output = fopen("result.xml","w"); 
fwrite($output,"<movies>"); 
foreach ($files as $f) 
{ 
    fwrite($output,file_get_contents($f)); 
} 
fwrite($output,"</movies>"); 
?> 
+1

這是一個完全精解*如果*沒有XML序言在文件中 – Gordon