【问题标题】:simplexml with multi line comment [closed]带有多行注释的 simplexml [关闭]
【发布时间】:2013-02-15 18:51:57
【问题描述】:

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

我试过这个:

<!--
     my comment
     fsdfs
  -->

但得到:评论未终止

我真的必须:

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

【问题讨论】:

标签: php xml xml-parsing simplexml


【解决方案1】:

您的示例似乎是有效的 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) {
    }
  }
}

【讨论】:

  • 我的注释行以 -- 开头,这导致错误以开头
  • 如何添加多个选项到:simplexml_load_file($filename, 'SimpleXMLElement', LIBXML_NOCDATA)
  • 我想尝试添加:`LIBXML_COMPACT
  • @JohnMagnolia 您不能在评论中的任何地方使用“--”;就是这样,而不是换行符,它破坏了你的代码。从技术上讲,终止评论的不是序列--&gt;-- 本身会立即终止它(当你进入细节时它会有点混乱,但最重要的是不要使用它在评论中排序!)
猜你喜欢
  • 2016-09-18
  • 2015-10-22
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 2013-10-05
  • 2019-05-27
  • 1970-01-01
  • 2023-04-04
相关资源
最近更新 更多