PHP中的XML标记计数

时间:2014-12-13 05:54:49

标签: php xml

<videos>
    <video>
        <title1>Title1</title1>
        <title2>Title2</title2>
        <title3>Title3</title3>
        <title4>Title4</title4>
        <desc> desc</desc>
        <img> img</img>
    </video>
    <video>
        <title1>Title1</title1>
        <title2>Title2</title2>
        <title3>Title3</title3>
        <desc> desc</desc>
        <img> img</img>
    </video>

    <video>
        <title1>Title1</title1>
        <title2>Title2</title2>
        <title3>Title3</title3> 
        <title4>Title3</title4> 
        <title5>Title3</title5>
        <desc> desc</desc>
        <img> img</img>
    </video>
</videos>

我有这种类型的xml。我想将标题计入一个视频标签。

还想创建文本幻灯片放映。我该怎么办?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以开始使用SimpleXML来解析xml,然后使用count()

$xml = simplexml_load_file('path/to/xmlfile.xml');
foreach($xml->video as $video) {
    $titles = $video->xpath('./*[contains(name(), "title")]');
    // $titles = $video->xpath('./*[substring(name(), 1, 5) = "title"]');
    $count = count($titles);
    echo $count .'<br/>';
    foreach($titles as $title) {
        echo $title , ' ';
    }
    echo '<br/>';
}