PHP DomDocument - 自闭标签和特殊字符

PHP DomDocument - self closing tag and special chars

我正在使用 PHPDomDocument 生成一个 XML 文件,但我在某一时刻遇到了一个包含 html 授权的自闭标签.

这是期望的输出:

<http-headers>
   <header name="Access-Control-Allow-Origin" value="*" />
</http-headers>

这就是我现在正在(错误地)做的事情:

$httpHeaders = $xml->createElement("http-headers");
$icecast->appendChild($httpHeaders);

$headerName = $xml->createTextNode('<header name="Access-Control-Allow-Origin" value="*" />');
$httpHeaders->appendChild($headerName);

这是给了我:

<http-headers>&lt;header name="Access-Control-Allow-Origin" value="*" /&gt;</http-headers>

我查看了 namespaces and attribute values,但这一切都非常令人困惑,而且我还未能找到添加自关闭标签的解决方案。

我还需要输出 <> 字符,而不是将它们转换为 &lt;&gt;

有人能给我指出正确的方向吗?

编辑

取得了一些进展,现在以正确的方式添加元素:

$httpHeaders = $xml->createElement("http-headers");
$icecast->appendChild($httpHeaders);
$httpHeadersHeader = $xml->createElement("header");
$httpHeaders->appendChild($httpHeadersHeader);
$httpHeadersHeader->setAttribute("name", '"Access-Control-Allow-Origin" value="*"'); 

但它输出的是 html 代码而不是字符:

<http-headers>
    <header name="&quot;Access-Control-Allow-Origin&quot; value=&quot;*&quot;"/>
</http-headers>

我在输出之前添加了 UTF-8 编码,但没有帮助:

$xml->encoding = 'UTF-8';
return $xml->save('php://output');

如何让它输出实际的符号而不是代码?

您将两个属性添加到一个属性值中,您需要将其分成两个调用,每个调用一个...

$httpHeadersHeader->setAttribute("name", "Access-Control-Allow-Origin"); 
$httpHeadersHeader->setAttribute("value", "*");