spice
spice

Reputation: 1512

PHP DomDocument - self closing tag and special chars

I'm generating an XML file using PHP's DomDocument but I've come unstuck at one point with a self closing tag containing html entitles.

This is the desired output :

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

This is what I'm (incorrectly) doing right now :

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

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

Which is giving me :

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

I've looked at namespaces and attribute values but it's all very confusing and also I haven't been able to find a solution that adds a self closing tag.

Also I need to output the < and > chars instead of converting them to &lt; and &gt;.

Could somebody point me in the right direction please?

EDIT

Made some progress, adding the element in the correct way now :

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

But it's outputting the html codes instead of the characters :

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

I'm adding UTF-8 encoding before outputting but it's not helping :

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

How do I make it output the actual symbols instead of the codes?

Upvotes: 0

Views: 255

Answers (1)

Nigel Ren
Nigel Ren

Reputation: 57131

Your adding both attributes into one attribute value, you need to split it into two calls, one for each...

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

Upvotes: 0

Related Questions