是否可以使用simplexml将注释标记插入到xml中?

时间:2022-11-27 17:24:27

I am using SimpleXML to build a document, and wondering whether it is possible to insert comment tag to the document like this:

我正在使用SimpleXML构建一个文档,我想知道是否可能向文档插入评论标记如下:

<root>
  <!-- some comment -->
  <value>
</root>

EDIT:

编辑:

The comment is somewhere in the middle of the document.

注释在文档的中间。

<root>
  <tag1 />
  <!-- some comment -->
  <value />
</root>

3 个解决方案

#1


6  

Unfortunately, SimpleXML doesn't handle comments. As it's been mentionned, DOM does handle comments but it's a kind of a bother to use for simple stuff, compared to SimpleXML.

不幸的是,SimpleXML不处理注释。正如前面提到的,DOM确实处理注释,但是与SimpleXML相比,对简单的东西使用它有点麻烦。

My recommendation: try SimpleDOM. It's an extension to SimpleXML, so everything works the same and it has a bunch of useful methods to deal with DOM stuff.

我的建议:SimpleDOM试试。它是SimpleXML的扩展,所以一切都是一样的,并且它有很多有用的方法来处理DOM内容。

For instance, insertComment($content, $mode) can append to or insert comments before or after a given node. For example:

例如,insertComment($content, $mode)可以在给定节点之前或之后追加或插入注释。例如:

include 'SimpleDOM.php';

$root = simpledom_load_string('<root><value/></root>');

$root->value->insertComment(' mode: append ', 'append');
$root->value->insertComment(' mode: before ', 'before');
$root->value->insertComment(' mode: after ', 'after');

echo $root->asPrettyXML();

...will echo

…将回声

<?xml version="1.0"?>
<root>
  <!-- mode: before -->
  <value>
    <!-- mode: append -->
  </value>
  <!-- mode: after -->
</root>

#2


5  

Nope, but apparently you can use DomDocument as a workaround (german):

没有,但显然你可以使用DomDocument作为解决方案(德语):

    $oNodeOld = dom_import_simplexml($oParent);
    $oDom = new DOMDocument();
    $oDataNode = $oDom->appendChild($oDom->createElement($sName));
    $oDataNode->appendChild($oDom->createComment($sValue));
    $oNodeTarget = $oNodeOld->ownerDocument->importNode($oDataNode, true);
    $oNodeOld->appendChild($oNodeTarget);
    return simplexml_import_dom($oNodeTarget);

But then again, why not use DOM directly?

但是,为什么不直接使用DOM呢?

#3


4  

There's actually a dirty trick, based on the fact that addChild doesn't check if the element name is valid:

实际上有一个肮脏的技巧,基于addChild不检查元素名是否有效的事实:

$root->addChild('!-- Your comment --><dummy');

When using $root->asXML() you'd get a string like this:

当使用$root->asXML()时,您会得到这样的字符串:

<root><!-- Your comment --><dummy/></root>

You may notice it generated an empty <dummy> element as well, but it's the price to pay. Don't try to add a value, it would only mess everything up. Use only in conjunction with asXML().

您可能会注意到,它生成了一个空的 <虚拟> 元素,但这是付出的代价。不要试图添加一个值,它只会把所有东西都弄乱。只与asXML()结合使用。

Well, I did say it's a dirty trick. I don't recommend using this in production, but only for debugging/testing purposes.

我的确说过这是卑鄙的伎俩。我不建议在生产中使用它,但只用于调试/测试目的。

#1


6  

Unfortunately, SimpleXML doesn't handle comments. As it's been mentionned, DOM does handle comments but it's a kind of a bother to use for simple stuff, compared to SimpleXML.

不幸的是,SimpleXML不处理注释。正如前面提到的,DOM确实处理注释,但是与SimpleXML相比,对简单的东西使用它有点麻烦。

My recommendation: try SimpleDOM. It's an extension to SimpleXML, so everything works the same and it has a bunch of useful methods to deal with DOM stuff.

我的建议:SimpleDOM试试。它是SimpleXML的扩展,所以一切都是一样的,并且它有很多有用的方法来处理DOM内容。

For instance, insertComment($content, $mode) can append to or insert comments before or after a given node. For example:

例如,insertComment($content, $mode)可以在给定节点之前或之后追加或插入注释。例如:

include 'SimpleDOM.php';

$root = simpledom_load_string('<root><value/></root>');

$root->value->insertComment(' mode: append ', 'append');
$root->value->insertComment(' mode: before ', 'before');
$root->value->insertComment(' mode: after ', 'after');

echo $root->asPrettyXML();

...will echo

…将回声

<?xml version="1.0"?>
<root>
  <!-- mode: before -->
  <value>
    <!-- mode: append -->
  </value>
  <!-- mode: after -->
</root>

#2


5  

Nope, but apparently you can use DomDocument as a workaround (german):

没有,但显然你可以使用DomDocument作为解决方案(德语):

    $oNodeOld = dom_import_simplexml($oParent);
    $oDom = new DOMDocument();
    $oDataNode = $oDom->appendChild($oDom->createElement($sName));
    $oDataNode->appendChild($oDom->createComment($sValue));
    $oNodeTarget = $oNodeOld->ownerDocument->importNode($oDataNode, true);
    $oNodeOld->appendChild($oNodeTarget);
    return simplexml_import_dom($oNodeTarget);

But then again, why not use DOM directly?

但是,为什么不直接使用DOM呢?

#3


4  

There's actually a dirty trick, based on the fact that addChild doesn't check if the element name is valid:

实际上有一个肮脏的技巧,基于addChild不检查元素名是否有效的事实:

$root->addChild('!-- Your comment --><dummy');

When using $root->asXML() you'd get a string like this:

当使用$root->asXML()时,您会得到这样的字符串:

<root><!-- Your comment --><dummy/></root>

You may notice it generated an empty <dummy> element as well, but it's the price to pay. Don't try to add a value, it would only mess everything up. Use only in conjunction with asXML().

您可能会注意到,它生成了一个空的 <虚拟> 元素,但这是付出的代价。不要试图添加一个值,它只会把所有东西都弄乱。只与asXML()结合使用。

Well, I did say it's a dirty trick. I don't recommend using this in production, but only for debugging/testing purposes.

我的确说过这是卑鄙的伎俩。我不建议在生产中使用它,但只用于调试/测试目的。