2012-03-08 79 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