0

我的问题是,我有一个 XML 内容。我用 PHP SimpleXMLElements() 类阅读它。XML 如下所示:

<search_result>
<movies>
     <movie>
        <title>Rambo 1</title>
     </movie>
     <movie>
        <title>Terminator 1</title>
     </movie>
     <movie>
        <title>Romeo and Juliett</title>
     </movie>
</movies>
<program>
    <item>
        <id>5405059</id>
        <show_date>03.22.</show_date>
        <show_time>14:30</show_time>
        <rfcdate>Fri, 22 Mar 2013 14:30:00 +0100</rfcdate>
        <title>Doku-cinema</title>
        <genre/>
        <channel_name>d1 Tv</channel_name>
        <channel_id>232</channel_id>
    </item>
    <item>
        <id>5405059</id>
        <show_date>03.22.</show_date>
        <show_time>14:30</show_time>
        <rfcdate>Fri, 22 Mar 2013 14:30:00 +0100</rfcdate>
        <title>Romantic-Romeo and Juliett</title>
        <genre/>
        <channel_name>d1 Tv</channel_name>
        <channel_id>232</channel_id>
    </item>
    <item>
        <id>5405059</id>
        <show_date>03.22.</show_date>
        <show_time>14:30</show_time>
        <rfcdate>Fri, 22 Mar 2013 14:30:00 +0100</rfcdate>
        <title>Doku-cinema</title>
        <genre/>
        <channel_name>d1 Tv</channel_name>
        <channel_id>232</channel_id>
    </item>
</program>

我想找到所有包含示例“罗马”的节点并只取回该节点。我的意思是……例如:

$xml = simplexml_load_file("moviecinemas.xml");
$searchNodes = $xml->xpath('//*[contains(.,'Romeo')]');

我想得到这个结构:

<item>
<id>5405059</id>
<show_date>03.22.</show_date>
<show_time>14:30</show_time>
<rfcdate>Fri, 22 Mar 2013 14:30:00 +0100</rfcdate>
<title>Romantic-Romeo and Juliett</title>
<genre/>
<channel_name>d1 Tv</channel_name>
<channel_id>232</channel_id>
</item>
<movie>
<title>Romeo and Juliett</title>
</movie>

我不知道这怎么可能,是的。

4

1 回答 1

1

对于我所看到的,您只是有一点 XPath 问题。顺便说一句,在您的问题中,示例 XML 格式错误,它没有search_result节点关闭标记。

这是一个工作示例:

<?php
$xml = <<<XML
<search_result>
    <movies>
        <movie>
            <title>Rambo 1</title>
         </movie>
         <movie>
            <title>Terminator 1</title>
         </movie>
         <movie>
            <title>Romeo and Juliett</title>
         </movie>
    </movies>
    <program>
        <item>
            <id>5405059</id>
            <show_date>03.22.</show_date>
            <show_time>14:30</show_time>
            <rfcdate>Fri, 22 Mar 2013 14:30:00 +0100</rfcdate>
            <title>Doku-cinema</title>
            <genre/>
            <channel_name>d1 Tv</channel_name>
            <channel_id>232</channel_id>
        </item>
        <item>
            <id>5405059</id>
            <show_date>03.22.</show_date>
            <show_time>14:30</show_time>
            <rfcdate>Fri, 22 Mar 2013 14:30:00 +0100</rfcdate>
            <title>Romantic-Romeo and Juliett</title>
            <genre/>
            <channel_name>d1 Tv</channel_name>
            <channel_id>232</channel_id>
        </item>
        <item>
            <id>5405059</id>
            <show_date>03.22.</show_date>
            <show_time>14:30</show_time>
            <rfcdate>Fri, 22 Mar 2013 14:30:00 +0100</rfcdate>
            <title>Doku-cinema</title>
            <genre/>
            <channel_name>d1 Tv</channel_name>
            <channel_id>232</channel_id>
        </item>
    </program>
</search_result>
XML;

$sxe        = new SimpleXMLElement($xml);
$romeoItems = $sxe->xpath('//program/item[contains(./title, "Romeo")]');

print_r($romeoItems);

输出:

Array
(
    [0] => SimpleXMLElement Object
        (
            [id] => 5405059
            [show_date] => 03.22.
            [show_time] => 14:30
            [rfcdate] => Fri, 22 Mar 2013 14:30:00 +0100
            [title] => Romantic-Romeo and Juliett
            [genre] => SimpleXMLElement Object
                (
                )
            [channel_name] => d1 Tv
            [channel_id] => 232
        )
)
于 2013-04-26T11:36:01.800 回答