XML元素包含非xml元素(未包装在cdata中)

时间:2011-04-12 08:31:12

标签: php xml simplexml

我正在使用简单的xml for php并尝试解析作为后续树的一部分的<content>元素:

<entry>

... <title>

... <link>

... <summary>

... <content>

问题是<content>元素不能作为字符串读取,因为它具有未包含在CDATA中的“非字符串”内容:

<content type="xhtml" xml:lang="en-US" xml:base="...">
<div xmlns="http://www.w3.org/1999/xhtml"><p><strong>text... text...</strong> text <a     href="..." 
target="_self">LINK</a>. text</p>
</content>

我使用以下代码:

$xml = simplexml_load_file($feed_uri);

然后:

foreach($xml->entry as $entry){
$item = new RSSItem();
$item->title = (string) $entry->title;
$item->content = (string)$entry->content;
$item->date = (string) $entry->published;

我的内容成员var为空。其他人如预期 如果我将 $ item-&gt; content =(string)$ entry-&gt; content; 更改为 $ item-&gt; content = $ entry - &gt; content;然后我得到另一个嵌套在 $ item-&gt;内容变量

中的对象

如何强制解析器不将内容元素(div,p等)中的元素解释为嵌套元素?

提前致谢, →

1 个答案:

答案 0 :(得分:2)

尝试$entry->content->asXML()

请参阅http://php.net/simplexmlelement.asxml