Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #5000 > unrolled thread

simplexml adding 'encoding' attribute to xml declaration

Started byBasewind <mike@pearl-group.com>
First post2012-02-24 19:17 -0800
Last post2012-02-27 10:34 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  simplexml adding 'encoding' attribute to xml declaration Basewind <mike@pearl-group.com> - 2012-02-24 19:17 -0800
    Re: simplexml adding 'encoding' attribute to xml declaration Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-02-25 20:40 +0100
    Re: simplexml adding 'encoding' attribute to xml declaration "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2012-02-27 10:34 +0100

#5000 — simplexml adding 'encoding' attribute to xml declaration

FromBasewind <mike@pearl-group.com>
Date2012-02-24 19:17 -0800
Subjectsimplexml adding 'encoding' attribute to xml declaration
Message-ID<23370133.287.1330139868500.JavaMail.geo-discussion-forums@pbcqw3>
I'm creating an xml file using simplexml, but would like to add the encoding="UTF-8" to the XML declaration.  Like this:

<?xml version="1.0" encoding="UTF-8"?>

How can I do this?

Here's my code:

<?php
$namesXML = new SimpleXMLElement("<names></names>");
echo $namesXML->asXML();

This shows:

<?xml version="1.0" ?><!-- want the encoding attribute added here -->
<names/>

Thanks!

[toc] | [next] | [standalone]


#5008

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2012-02-25 20:40 +0100
Message-ID<1355583.WGnKmheAe9@PointedEars.de>
In reply to#5000
Basewind wrote:

> I'm creating an xml file using simplexml, but would like to add the
> encoding="UTF-8" to the XML declaration.  Like this:
> 
> <?xml version="1.0" encoding="UTF-8"?>
> 
> How can I do this?
> 
> Here's my code:
> 
> <?php
> $namesXML = new SimpleXMLElement("<names></names>");
> echo $namesXML->asXML();
> 
> This shows:
> 
> <?xml version="1.0" ?><!-- want the encoding attribute added here -->
> <names/>

It is called *Simple*XML*Element* for a reason.  However, this particular 
character encoding declaration is superfluous; UTF-8 is one of the XML 
default encodings [1].

And do use header() to declare the XML media type that fits your data best.  
Keep in mind that PHP's default is text/html which is not appropriate for 
that kind of output.


PointedEars
___________
[1] <http://www.w3.org/TR/REC-xml/#charencoding>
-- 
var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
)  // Plone, register_function.js:16

[toc] | [prev] | [next] | [standalone]


#5022

From"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid>
Date2012-02-27 10:34 +0100
Message-ID<jifinu$3em$1@dont-email.me>
In reply to#5000
El 25/02/2012 4:17, Basewind escribió/wrote:
> I'm creating an xml file using simplexml, but would like to add the encoding="UTF-8" to the XML declaration.  Like this:
>
> <?xml version="1.0" encoding="UTF-8"?>
>
> How can I do this?
>
> Here's my code:
>
> <?php
> $namesXML = new SimpleXMLElement("<names></names>");
> echo $namesXML->asXML();
>
> This shows:
>
> <?xml version="1.0" ?><!-- want the encoding attribute added here -->
> <names/>

As far as I know, the only way is providing the full <?xml...?> tag in 
the constructor:

$namesXML = new SimpleXMLElement('<?xml version="1.0" 
encoding="utf-8"?><names></names>');
echo $namesXML->asXML();

SimpleXML will display the tag as-is (though it will not actually _use_ 
the encoding information). It'll use UTF-8 no matter what you type there.


-- 
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web