PHP forum

Simplexml adding 'encoding' attribute to xml declaration

Feb 25, 2012 3:17 am
Basewind

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!

Feb 25, 2012 7:40 pm
Thomas 'PointedEars' Lahn
Re: simplexml adding 'encoding' attribute to xml declaration

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
Feb 27, 2012 9:34 am
?lvaro G. Vicario
Re: simplexml adding 'encoding' attribute to xml declaration

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
--


Previous Thread: Chat on web, Ajax ?
Next Thread: Php installations

Related Forum Topics
SimpleXml select element by attribute value
Hi.

I'm trying to use simpleXml to parse a KML file and save some of the
KML data to a MySQL database.

I want to select a 'Style' element that has an attribute 'id' equal to
my PHP variable '$styleUrl.

A snippet of the KML to show how the Style elements are structured:

<Document>
...
Retrieve all HTML elements, their attributes, attribute values, and text from a string?
I was wondering if anyone out there had a function/ class available
they would be willing to share which could retrieve all HTML elements
from a string and for each element, retrieve all attributes, all
attribute values, and whatever text is contained within that element?

TIA


Php code for ajax encoding
I am trying to retrieve text from the my database using ajax code
witten in php.

Somewhere its getting twisted. All the text is retrieved and displayed
correctly except the Swedish characters used in some of the words that
are stored in my mysql phpmyadmin database.

These are displayed...
New DateTimeZone and html encoding
when trying to do this :
$oldTZone= new DateTimeZone("Pacific Time (US & Canada");
or
$oldTZone=timezone_open("Pacific Time (US & Canada")
i get
Unknown or bad timezone (Pacific Time (US &amp; Canada))

it seems that it encodes the &. How can i disable it from doing so?



Re: new DateTimeZone and html encoding
Fairly certain what's going on here is PHP is html-encoding the & in the warning on the way out. It's not an issue of DateTime encoding it on the way in.

Your DateTime constructor is more likely failing because of the unmatched parenthesis you're sending it: "Pacific Time (US & Canada"


Re: new DateTimeZone and html encoding
On Friday, March 4, 2011 11:28:32 AM UTC-5, Erwin Moller wrote:
> On 3/4/2011 3:55 PM, matt wrote:
> > Fairly certain what's going on here is PHP is html-encoding the& in the warning on the way out. It's not an issue of DateTime encoding it on the way in.
> >
> > Your DateTime constructor...
Simplexml puzzle
I am calling the google maps geocode APIs to retrieve address
information. The call returns an xml structure. I am using simplexml
to decode it and work with it. Here's the relevant code segment:

$data = curl_exec($ch); //retrieve data
curl_close($ch); //close curl
$xml =...
SimpleXML parsing need help
So I just don't get it.

here is a part of the xml code:
......
<weather>
<date>2010-02-11</date>
<tempMaxC>4</tempMaxC>
<tempMaxF>40</tempMaxF>
........

i can get the date with
echo " date",$xml->weather[0]->date;

so how comes that i can't get

echo " max temperatura...
Simplexml - remove item
Hi,

I have a tree parsed with SimpleXML, and now I want to remove one item from
the list of objects and re-write the file. How do I drop one?

--
------------------------------------
Gernot Frisch
http://www.glbasic.com



Problem with quoted printable encoding in PEAR Mail
Hello,
I am sending email usinh PEAR. There's a problem with Polish
characters: I see additional strings 3D and =. I learned that it is
quoted-printable encoding. I include source code.
Note that in functionsendEmail() I have 'text/html;
charset=ISO-8859-2', function emailMessage() also...