如何将XML节点的内容作为文本获取

时间:2018-01-15 11:44:03

标签: php simplexml

<?php

$string = <<<XML
    <a>
     <y>
      This is <b>text</b>
      <c>stuff</c>
     </y>
     <d>
       This is super <em>code</em>, and we like it ! <b>FOObarr</b> !
     </d>
    </a>
XML;

$xml = new SimpleXMLElement($string);

foreach ($xml as $node) {
    //use another function than asXML, to get some more magic
    echo $node->asXML();
    echo "\n--\n";
}
?>

我想输出这个:

      This is <b>text</b>
      <c>stuff</c>
--
      This is super <em>code</em>, and we like it ! <b>FOObarr</b> !
--

没有y标签和d标签;但是当前代码输出:

<y>
      This is <b>text</b>
      <c>stuff</c>
     </y>
--
<d>
       This is super <em>code</em>, and we like it ! <b>FOObarr</b> !
     </d>
--

请注意,根据输入数据,标签的名称可能会有所不同。

2 个答案:

答案 0 :(得分:1)

正如@iainn所说,你在HTML中嵌入html而不编码它,所以它会变得有点混乱。但根据您的示例,您可以在回显之前更深入地导航一个级别,因为HTML看起来像解码器的XML。

<?php

$string = <<<XML
        <a>
         <y>
          This is <b>text</b>
          <c>stuff</c>
         </y>
         <d>
           This is super <em>code</em>
         </d>
        </a>
XML;

$xml = new SimpleXMLElement($string);

foreach ($xml as $node) {
    echo $node;
    foreach($node as $subnode) {
        echo $subnode->asXml();
    }
    echo "\n--\n";
}
?>

产地:

      This is 

     <b>text</b><c>stuff</c>
--

       This is super 
     <em>code</em>
--

如果在实践中您的数据有点复杂或者换行和其他细微之处很重要,请考虑对HTML进行编码。

答案 1 :(得分:-1)

<?php

$string = <<<XML
    <a>
     <y>
      This is <b>text</b>
      <c>stuff</c>
     </y>
     <d>
       This is super <em>code</em>, and we like it ! <b>FOObarr</b> !
     </d>
    </a>
XML;

$xml = new SimpleXMLElement($string);

foreach ($xml as $node) {
    $s = trim($node->asXML());
    $s = preg_replace(['#^<[^>]*>#','#<[^>]*>$#'], '', $s);
    $s = trim($s);
    echo $s;
    echo "\n--\n";
}
?>