在php中使用simplexml无法获取节点值

时间:2014-10-13 17:00:48

标签: php xml simplexml

我有以下XML文件

<?xml version="1.0" encoding="UTF-8"?>
<newsItem>
       <contentSet>
              <inlineXML contenttype="application/xhtml+xml">
                     <html xmlns="http://www.w3.org/1999/xhtml">
                            <div>
                                   <h1>St. Augustine Gold and Copper Limited: Update on Recent Corporate Developments</h1>
                               </div>
                      </html>
               </inlineXML>
       </contentSet>
</newsItem>

我希望使用以下代码获取值,没有问题

if (file_exists('example.newsml')) {
    $xml = simplexml_load_file('example.newsml');

    $html= (string) $xml->{'contentSet'}->{'inlineXML'}->{'html'}->{'div'}->{'h1'};

    echo $html; 

} else {
    exit('Failed to open test.xml.');
}

我试图从节点获取html,但结果为空。

$content = (string) $xml->{'contentSet'}->{'inlineXML'}->{'html'};
echo $content;

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

只需将节点另存为XML

echo $xml->contentSet->inlineXML->html->saveXml();

输出:https://eval.in/205477

<html xmlns="http://www.w3.org/1999/xhtml">
  <div>
    <h1>St. Augustine Gold and Copper Limited: Update on Recent Corporate Developments</h1>
  </div>
</html>