SimpleXML 不断返回 CDATA 元素上的内容

发布于 2025-01-08 20:44:18 字数 2549 浏览 0 评论 0原文

这是另一个 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

                                                    )

                                            )

                                    )

                            )

                    )

            )

我尝试在元素上使用 (string) 或不使用 (string) 。

我也尝试过使用

$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');

So another CDATA returning content question. I've seen many answers, but even though I tried them all, I still get only content.

In more details:

I have an xml file (containing many NewsItem inside):

<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>

I am trying to get the content of body.content.

Here is my code:

$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>';

My echo returns:
content

even though I do see the content in the print_r of my xml, as we can see here:

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

                                                    )

                                            )

                                    )

                            )

                    )

            )

I tried using (string) or not on the element.

I also tried using

$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');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

各空 2025-01-15 20:44:18

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

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

For element names which cannot be PHP identifiers (like body.content), you must use an alternative PHP notation:

$element->NewsComponent->ContentItem->DataContent->body->{'body.content'};
乄_柒ぐ汐 2025-01-15 20:44:18

我认为你的示例返回“content”,因为你将一个不存在的元素

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

与字符串“content”连接起来 - 可能 PHP 抱怨名称 content 没有常量,因此假设你的意思是“content”。

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

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

I think your example returns 'content' because you are concatenating an element that does not exist

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

with the string 'content' - probably PHP complains that there's no constant with the name content and therefore assumes you meant 'content'.

Thus my guess is you need to find another way to select an element with a dot in the name.

(This problem does not appear to be related to CDATA.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文