2012-03-08 7 views
1

괜찮습니다. 이것이 나를 미치게합니다.
XML 파일을 특정 배열이나 개체로 구문 분석하려고 했으므로 유사한 파일과 비교하여 차이점을 테스트 할 수 있습니다.
그러나 나는 운이 없었습니다. 나는 이것을하기 위해 SimpleXMLIterator와 SimpleXMLElement를 사용하려고 시도해왔다.
다음은 몇 가지 샘플입니다 : 여기 비교를 위해 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; 

내가 느끼는 ... 평소처럼 :) 내 오류를 발견 [편집] , 내가 트립되었다 object의 배열에 대해서. 원하는 방식으로 작동하도록 문자열로 원하는 데이터를 캐스팅해야했습니다. 그냥 여기에 정보를 원하시면 내가 원하는 궤도에 나를 넣어하는 해낸 것입니다 :

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

답변

0

XPATH는 찾고있는 대답입니다. 영화 노드에 자식이 없을 때 영화 노드 아래에 영화 노드를 찾고 있기 때문에 XPATH가 작동하지 않는 이유가 있다고 생각합니다.

편집 : 반복기 대신 foreach 루프를 사용하는 것이 더 쉬울 수도 있다고 생각하십시오. 전에는 본 적이 없었던 반복기를 찾아야했습니다. simplexml과 xpath를 잠시 사용하고 있습니다. 또한 XML 편집도 계획하고 있다면 SimpleXMLElement 만 사용해야한다고 생각합니다. 비교를 위해 단순히 읽고 싶다면 simplexml_load_file을 사용하는 것이 가장 좋습니다. xpath를 간단하게 변경할 수도 있습니다.

xpath('//movie'); 
+0

xpath가 작동했습니다. 나는 나머지를 알아 내면 돌아올거야. – RockyFord

0

당신은 단지 전체 파일 내용을 비교 문자열로 두 파일의 내용을 읽고 문자열을해야하는 경우 비교. 그렇지 않으면 노드의 innerXML을 가져 와서 문서의 하위 수준에서 동일한 작업을 수행 할 수 있습니다.

+0

죄송합니다. 내 물건을 추가하려고했습니다. 즉시 제거했습니다. – mseancole

+0

걱정할 필요가 없습니다. 편집을 거부했습니다. – aaroncatlin

관련 문제