хорошо, это сводит меня с ума.
Я пытался разобрать файл XML в определенный массив или объект, чтобы сравнить его с аналогичным файлом, чтобы проверить различия.
Однако мне не повезло. Я пытался использовать SimpleXMLIterator и SimpleXMLElement, чтобы сделать это.
Вот несколько примеров:
<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');
}