【问题标题】:PHP DomDocument - self closing tag and special charsPHP DomDocument - 自结束标记和特殊字符
【发布时间】:2019-02-20 19:01:59
【问题描述】:

我正在使用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>

我查看了 namespacesattribute values 但这一切都非常令人困惑,而且我还没有找到添加自结束标记的解决方案。

我还需要输出&amp;lt;&amp;gt; 字符,而不是将它们转换为&amp;lt;&amp;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');

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

【问题讨论】:

    标签: php xml domdocument


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-09-28
      • 2019-07-08
      • 1970-01-01
      • 1970-01-01
      • 2013-08-25
      • 2011-04-23
      • 2013-10-14
      • 1970-01-01
      • 2018-02-05
      相关资源
      最近更新 更多