2012-03-08 45 views
1

好吧,這讓我瘋狂。
我一直在試圖解析一個xml文件到一個特定的數組或對象,所以我可以將它與一個類似的文件進行比較以測試差異。
但是我沒有運氣。我一直在試圖使用SimpleXMLIterator和SimpleXMLElement來做到這一點。
下面是一些樣本:Parse Xml文件比較

<xml> 
//This is the first record of 1073 
    <viddb> 
     <movies>1074</movies> 
     <movie> 
      <title>10.5</title> 
      <origtitle>10.5</origtitle> 
      <year>2004</year> 
      <genre>Disaster</genre> 
      <release></release> 
      <mpaa></mpaa> 
      <director>John Lafia</director> 
      <producers>Howard Braunstein, Jeffrey Herd</producers> 
      <actors>Kim Delaney, Fred Ward, Ivan Sergei</actors> 
      <description>An earthquake reaching a 10.5 magnitude on the Richter scale, strikes the west coast of the U.S. and Canada. A large portion of land falls into the ocean, and the situation is worsened by aftershocks and tsunami.</description> 
      <path>E:\www\Media\Videos\Disaster\10.5.mp4</path> 
      <length>164</length> 
      <size>3648</size> 
      <resolution>640x272</resolution> 
      <framerate>29.97</framerate> 
      <videocodec>AVC</videocodec> 
      <videobitrate>2966</videobitrate> 
      <label>Roku Media</label> 
      <poster>images/10.5.jpg</poster> 
     </movie> 

下面是這個記錄產生使用對象$iter = new SimpleXMLIterator($xml, 0, TRUE);

object(SimpleXMLIterator)#71 (1) { 
    ["viddb"] => object(SimpleXMLIterator)#72 (2) { 
    ["movies"] => string(4) "1074" 
    ["movie"] => array(1074) { 
     [0] => object(SimpleXMLIterator)#73 (19) { 
     ["title"] => string(4) "10.5" 
     ["origtitle"] => string(4) "10.5" 
     ["year"] => string(4) "2004" 
     ["genre"] => string(8) "Disaster" 
     ["release"] => object(SimpleXMLIterator)#1158 (0) { 
     } 
     ["mpaa"] => object(SimpleXMLIterator)#1159 (0) { 
     } 
     ["director"] => string(10) "John Lafia" 
     ["producers"] => string(31) "Howard Braunstein, Jeffrey Herd" 
     ["actors"] => string(35) "Kim Delaney, Fred Ward, Ivan Sergei" 
     ["description"] => string(212) "An earthquake reaching a 10.5 magnitude on the Richter scale, strikes the west coast of the U.S. and Canada. A large portion of land falls into the ocean, and the situation is worsened by aftershocks and tsunami." 
     ["path"] => string(37) "E:\www\Media\Videos\Disaster\10.5.mp4" 
     ["length"] => string(3) "164" 
     ["size"] => string(4) "3648" 
     ["resolution"] => string(7) "640x272" 
     ["framerate"] => string(5) "29.97" 
     ["videocodec"] => string(3) "AVC" 
     ["videobitrate"] => string(4) "2966" 
     ["label"] => string(10) "Roku Media" 
     ["poster"] => string(15) "images/10.5.jpg" 
     } 

我試圖生產(目前)是每一個級別關聯數組電影。我所閱讀和遵循的所有示例總是產生一組數組,這些數組更難以處理。

這是我在:

$iter = new SimpleXMLIterator($xml, 0, TRUE); 
     Zend_Debug::dump($iter); 
     //so far xpath has not worked for me, I can't get $result to return anything 
     $result = $iter->xpath('/xml/viddb/movies/movie'); 
     $movies = array(); 
     for ($iter->rewind(); $iter->valid(); $iter->next()) { 
      foreach ($iter->getChildren() as $key => $value) { 
       //I can get each movie title to echo but when I try to put them into an 
       // array it only has the last record 
       echo $value->title . '<br />'; 
       $movies['title'] = $value->title; 

      } 
     } 
     return $movies; 

我覺得我失去了一些東西簡單明瞭......像往常一樣:) [編輯] 我發現我的錯誤,我被絆倒在對象的數組之上。我不得不把我想要的數據作爲一個字符串來使其工作,我想如何。只是這裏的信息是什麼,我想出了把我想要的軌道上:

public function indexAction() { 
     $xml = APPLICATION_PATH . '/../data/Videos.xml'; 
     $iter = new SimpleXMLElement($xml, 0, TRUE); 
     $result = $iter->xpath('//movie'); 

     $movies = array(); 
     foreach ($result as $key => $movie) { 
      $movies[$key + 1] = (string) $movie->title; 
     } 
     Zend_Debug::dump($movies, 'Movies'); 
    } 
+0

你嘗試過'$ movies ['title'] [] = $ value-> title;'? – maialithar 2012-03-08 14:15:23

回答

0

XPATH是你正在尋找的答案。我認爲你的XPATH不工作的原因是因爲當電影節點沒有任何孩子時,你正在電影節點下尋找一個電影節點。

編輯:認爲使用foreach循環代替迭代器可能更容易。我不得不查看迭代器,因爲我從來沒有見過它。過一段時間也使用了simplxml和xpath。另外,如果您計劃編輯XML,我相信您應該只使用SimpleXMLElement。如果您只是想閱讀比較,最好使用simplexml_load_file。你也可以簡單地改變你的xpath。

​​
+0

xpath工作。我找出剩下的部分後我會回來。 – RockyFord 2012-03-09 06:27:10

0

如果你只是需要比較整個文件內容,閱讀這兩個文件的內容轉換爲字符串,並做一個字符串比較。否則,您可以通過獲取任何節點的innerXML在文檔的較低級別執行相同操作。

+0

對不起,我正在嘗試添加一些東西給我。我立即刪除它 – mseancole 2012-03-08 14:47:28

+0

不用擔心,我拒絕了編輯。 – aaroncatlin 2012-03-08 14:54:48