SimpleXML 获取元素的原始值

SimpleXML Getting raw value of element

我有以下 XML 代码并且我 可以使用 SimpleXML:

<CATEGORY_TREE>/Gry&#47;Puzzle/puzzle/</CATEGORY_TREE>
<CATEGORY_NAME>/Gry&#47;Puzzle/puzzle/</CATEGORY_NAME>

$node->CATEGORY_TREE returns /Gry/Puzzle/puzzle/ 正确。

有什么方法可以获取原始值/Gry&#47;Puzzle/puzzle/

使用 html_entity_decode() 函数:

<?php
echo html_entity_decode($node->CATEGORY_TREE);

仅通过不使用基于 LibXML2 的 XML 解析器(SimpleXML、DOM、XMLReader)。它们都强制替换预定义的实体。

来自LibXML2 documentation

Note that at save time libxml2 enforces the conversion of the predefined entities where necessary to prevent well-formedness problems, and will also transparently replace those with chars (i.e. it will not generate entity reference elements in the DOM tree or call the reference() SAX callback when finding them in the input).

对于 XML 解析器,“/”和“+”是相等的。要将实体存储在 XML 中,必须对其 & 进行转义。如果您在 DOM 中将其创建为文本节点,您会看到 DOM 会为序列化执行此操作:

$document = new DOMDocument();
$document
  ->appendChild($document->createElement('CATEGORY_TREE'))
  ->appendChild($document->createTextNode('/Gry&#47;Puzzle/puzzle/'));
echo $document->saveXml();

输出:

<?xml version="1.0"?>
<CATEGORY_TREE>/Gry&amp;#47;Puzzle/puzzle/</CATEGORY_TREE>