解析Xml文件以进行比较

时间:2012-03-08 14:11:40

标签: php xml zend-framework iterator

好吧这让我发疯了。
我一直在尝试将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');
    }

2 个答案:

答案 0 :(得分:0)

如果您只需要比较整个文件内容,请将两个文件的内容读入字符串并进行字符串比较。否则,您可以通过获取任何节点的innerXML在文档的较低级别执行相同操作。

答案 1 :(得分:0)

XPATH是您正在寻找的答案。我认为你的XPATH无法工作的原因是因为当电影节点没有任何孩子时你正在电影节点下寻找一个电影节点。

编辑:认为使用foreach循环而不是迭代器可能更容易。我不得不查看迭代器,因为我以前从未见过它。一直在使用simplxml和xpath。另外,我相信如果您计划编辑XML,也应该只使用SimpleXMLElement。如果您只是想阅读它以进行比较,最好使用simplexml_load_file。您也可以简单地将xpath更改为。

xpath('//movie');