【问题标题】:SimpleXML keeps returning content on CDATA elementSimpleXML 不断返回 CDATA 元素上的内容
【发布时间】:2012-02-23 21:14:00
【问题描述】:

所以另一个 CDATA 返回内容问题。我已经看到了很多答案,但即使我都尝试了它们,我仍然只能得到内容。

更多细节:

我有一个 xml 文件(里面包含很多 NewsItem):

<NewsML>
<NewsItem>    
    <NewsComponent>               
        <ContentItem>                      
            <DataContent>                
                <body>                    
                    <body.content>                        
                        <![CDATA[<p>This is what I am trying to retrieve</p>]]>
                    </body.content>
                </body>
            </DataContent>
        </ContentItem>
    </NewsComponent>
</NewsItem>

我正在尝试获取 body.content 的内容。

这是我的代码:

$xml = simplexml_load_file('path/to/my/xml.xml',null,LIBXML_NOCDATA);

if(count($xml->children()) > 0){
    foreach($xml->children() as $element){
        $description = (string)$element->NewsComponent->ContentItem->DataContent->body->body.content;
        echo $description;
    }
}
echo '<pre>';
print_r($xml);
echo '</pre>';

我的回声返回: 内容

尽管我确实在我的 xml 的 print_r 中看到了内容,但我们可以在这里看到:

SimpleXMLElement Object
(
    [NewsItem] => Array
    (
        [0] => SimpleXMLElement Object
            (

                [NewsComponent] => SimpleXMLElement Object
                    (                            

                        [ContentItem] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (

                                        [DataContent] => SimpleXMLElement Object
                                            (
                                                [body] => SimpleXMLElement Object
                                                    (
                                                        [body.content] => This is what I am trying to retieve

                                                    )

                                            )

                                    )

                            )

                    )

            )

我尝试在元素上使用(字符串)或不使用。

我也尝试过使用

$xml = simplexml_load_file('path/to/my/xml.xml',null,LIBXML_NOCDATA);
vs
$xml = simplexml_load_file('path/to/my/xml.xml',"SimpleXMLElement",LIBXML_NOCDATA);
vs
$xml = simplexml_load_file('path/to/my/xml.xml');

【问题讨论】:

    标签: php xml simplexml cdata


    【解决方案1】:

    对于不能是 PHP 标识符的元素名称(例如 body.content),您必须使用替代的 PHP 表示法:

    $element->NewsComponent->ContentItem->DataContent->body->{'body.content'};
    

    【讨论】:

      【解决方案2】:

      我认为您的示例返回“内容”,因为您正在连接一个不存在的元素

      $element->NewsComponent->ContentItem->DataContent->body->body
      

      使用字符串 'content' - 可能 PHP 抱怨名称 content 没有常量,因此假定您的意思是 'content'。

      因此我的猜测是您需要找到另一种方法来选择名称中带有点的元素。

      (此问题似乎与 CDATA 无关。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-05
        • 2015-04-26
        • 2015-03-08
        • 1970-01-01
        • 1970-01-01
        • 2013-06-19
        • 2014-08-20
        相关资源
        最近更新 更多