如何使用SimpleXML解析XML的CDATA HTML内容?

use*_*819 3 html php xml rss simplexml

我正在尝试将Xml内容显示到表中,所有工作都很完美,但标签中的一些内容我不想显示,我只想要图像但不是

2012年11月日历从5.10测试

像在xml中一样

 <content:encoded><![CDATA[<p>November 2012 calendar from 5.10 The Test</p>
    <p><a class="shutterset_" href='http://trance-gemini.com/wordpress/wp-content/gallery/calendars/laura-bertram-trance-gemini-145-1080.jpg' title='&lt;br&gt;November 2012 calendar from 5.10 The Test&lt;br&gt; &lt;a href=&quot;</a></p>]]>
</content:encoded> 
Run Code Online (Sandbox Code Playgroud)

我想显示图像但不是

2012年11月日历从5.10测试

.

<?php
// load SimpleXML
$item = new SimpleXMLElement('test1.xml', null, true);

echo <<<EOF
<table border="1px">
        <tr cl>

        </tr>       
EOF;
foreach($item->channel->item as $boo) // loop through our books
{
        echo <<<EOF

         <tr>
            <td rowspan="3">{$boo->children('content', true)->encoded}</td>
            <td>{$boo->title}</td>   
        </tr>

        <tr>
           <td>{$boo->description}</td>
        </tr>

        <tr>
           <td>{boo->comments}</td>
        </tr>
EOF;
}
echo '</table>';
?>
Run Code Online (Sandbox Code Playgroud)

hak*_*kre 6

我曾经回答过,但我找不到答案了.

如果你看一下字符串(简化/美化):

<content:encoded><![CDATA[
    <p>Lorem Ipsom</p>
    <p>
      <a href='laura-bertram-trance-gemini-145-1080.jpg' 
         title='&lt;br&gt;November 2012 calendar from 5.10 The Test&lt;br&gt; &lt;a href=&quot;</a>
    </p>]]>
</content:encoded> 
Run Code Online (Sandbox Code Playgroud)

您可以看到在元素的节点值内部编码了HTML <content:encoded>.首先,您需要获取已经执行的HTML值:

$html = $boo->children('content', true)->encoded;
Run Code Online (Sandbox Code Playgroud)

然后你需要在里面解析HTML $html.使用PHP可以使用哪些库进行HTML解析,概述如下:

如果您决定使用或多或少的推荐DOMDocument作业,您只需要获取某个元素的属性值:

或者你已经使用它的姐妹库SimpleXML(所以这个更推荐,请参阅下一节):


在您的问题的上下文中,以下提示:

你正在使用SimpleXML.DOMDocument是一个姐妹库,意味着您可以在两者之间进行交换,因此您无需学习全新的库.

例如,您只能使用HTML解析功能DOMDocument,但可以将其导入SimpleXML.这很有用,因为SimpleXML不支持HTML解析.

这是有效的simplexml_import_dom().

简化的分步示例:

// get the HTML string out of the feed:
$htmlString = $boo->children('content', true)->encoded;

// create DOMDocument for HTML parsing:
$htmlParser = new DOMDocument();

// load the HTML:
$htmlParser->loadHTML($htmlString);

// import it into simplexml:
$html = simplexml_import_dom($htmlParser);
Run Code Online (Sandbox Code Playgroud)

现在,您可以将其$html用作表示HTML文档的新SimpleXMLElement.由于您的HTML块没有任何<body>标记,根据HTML规范,它们被放在<body>标记内.这将允许您例如访问示例中第二个元素内的第href一个属性:#<a><p>

// access the element you're looking for:
$href = $html->body->p[1]->a['href'];
Run Code Online (Sandbox Code Playgroud)

这里是上面的完整视图(在线演示):

// get the HTML string out of the feed:
$htmlString = $boo->children('content', true)->encoded;

// create DOMDocument for HTML parsing:
$htmlParser = new DOMDocument();

// your HTML gives parser warnings, keep them internal:
libxml_use_internal_errors(true);

// load the HTML:
$htmlParser->loadHTML($htmlString);

// import it into simplexml:
$html = simplexml_import_dom($htmlParser);

// access the element you're looking for:
$href = $html->body->p[1]->a['href'];

// output it
echo $href, "\n";
Run Code Online (Sandbox Code Playgroud)

它输出的是什么:

laura-bertram-trance-gemini-145-1080.jpg
Run Code Online (Sandbox Code Playgroud)

  • 我只添加一件事:为了稍后的理智,在 `$htmlString = (string)$boo ...` 和 `$href = (string)$html ...` 中添加一个显式的 `(string)`。我发现在不需要时总是添加它的精神开销比调试那些需要但您没有意识到的情况的开销要少。 (2认同)