带有多行注释的simplexml

时间:2013-02-15 18:51:58

标签: php xml xml-parsing simplexml

使用simplexml通过php解析时,是否可以在xml中有多行注释?

我试过这个:

<!--
     my comment
     fsdfs
  -->

但是得到:评论没有终止

我真的需要:

<!-- my comment -->
<!-- fsdfs -->

1 个答案:

答案 0 :(得分:0)

您的示例似乎是有效的XML,不应生成错误。下面的代码已经过测试,并且在PHP v5.4.7中没有生成任何错误或警告。

<?php
error_reporting(E_ALL | E_STRICT);

$string = <<< EOT
<xml>
    <foo>Hello</foo>
    <bar>World</bar>
    <!-- This is a single line comment. -->
    <!--
    This is a
    multi-line comment.
    -->
</xml>
EOT;

$xml = simplexml_load_string($string);

echo '<pre>';
var_dump($xml);

输出:

object(SimpleXMLElement)#1 (3) {
  ["foo"]=>
  string(5) "Hello"
  ["bar"]=>
  string(5) "World"
  ["comment"]=>
  array(2) {
    [0]=>
    object(SimpleXMLElement)#2 (0) {
    }
    [1]=>
    object(SimpleXMLElement)#3 (0) {
    }
  }
}
相关问题