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


Can not get node value using simplexml in php

我有以下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;

有什么建议吗?

只需将节点保存为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>