XML encoding

This is a discussion on XML encoding within the PHP Language forums, part of the PHP Programming Forums category; Hi, I've written a PHP class which can output a UK position as XML. The code snippet is as ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 12-14-2007
Matthew
 
Posts: n/a
Default XML encoding

Hi,

I've written a PHP class which can output a UK position as XML.

The code snippet is as follows:

$crlf = "\r\n";

$xml = "";
$xml = $xml."<?xml version=\"1.0\" encoding=\"UTF-8\"?>".$crlf;
$xml = $xml."<UK_Location xmlns:xsi=\"www.w3.org\">".$crlf;
$xml = $xml."<OS_X>".$this->osX."</OS_X>".$crlf;
....snip...
// Convert the XML into the UTF-8 text encoding scheme
$xml = utf8_encode($xml);

The output from Firefox is as expected and as I want:

<?xml version="1.0" encoding="UTF-8"?>
<UK_Location xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Postcode>W1D 5BT</Postcode>
<OS_X>529827</OS_X>
<OS_Y>180975</OS_Y>
<Latitude>N51:30:46</Latitude>
<Longitude>W0:07:50</Longitude>
<Latitude_Decimal>51.512793</Latitude_Decimal>
<Longitude_Decimal>-0.130487</Longitude_Decimal>
</UK_Location>

But from IE, I get a '-' at the start of the 2nd line.

<?xml version="1.0" encoding="UTF-8" ?>
- <UK_Location xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Postcode>W1D 5BT</Postcode>

This makes the XML invalid. Why do I get this and what can I do about it?
Please note the '-' is there even if I leave the utf8_encode() line out,
my initial supposition was that the utf8_encode() was causing the problem.

Thanks.
Reply With Quote
  #2 (permalink)  
Old 12-14-2007
Matthew
 
Posts: n/a
Default Re: XML encoding

however I just discovered if I save the file from IE, the '-' disappears!
This PC only has IE 6.0 on it, might it just be an older version of IE XML
rendering problem?
Reply With Quote
  #3 (permalink)  
Old 12-14-2007
Willem Bogaerts
 
Posts: n/a
Default Re: XML encoding

> however I just discovered if I save the file from IE, the '-'
> disappears! This PC only has IE 6.0 on it, might it just be an older
> version of IE XML rendering problem?


No, it is IE's way of telling you that you can unfold that region. Just
click the '-'.

Have a nice weekend,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Reply With Quote
  #4 (permalink)  
Old 12-14-2007
Matthew
 
Posts: n/a
Default Re: XML encoding

Willem Bogaerts emailed this:
>> however I just discovered if I save the file from IE, the '-'
>> disappears! This PC only has IE 6.0 on it, might it just be an older
>> version of IE XML rendering problem?

>
> No, it is IE's way of telling you that you can unfold that region. Just
> click the '-'.
>
> Have a nice weekend,


Oops. Spot the person who almost never uses IE !!

Thanks for that.
Reply With Quote
  #5 (permalink)  
Old 12-14-2007
Michael Fesser
 
Posts: n/a
Default Re: XML encoding

..oO(Matthew)

>Willem Bogaerts emailed this:
>>> however I just discovered if I save the file from IE, the '-'
>>> disappears! This PC only has IE 6.0 on it, might it just be an older
>>> version of IE XML rendering problem?

>>
>> No, it is IE's way of telling you that you can unfold that region. Just
>> click the '-'.
>>
>> Have a nice weekend,

>
>Oops. Spot the person who almost never uses IE !!


Just some additional "style hints":

1) Is there any specific need for a CRLF line break? Usually a LF ("\n")
should be more than enough. You could also define your line break as a
constant, which is what I usually prefer in such cases:

define('CRLF', "\r\n");

2) There are many ways to write an XML string with embedded variables.
Personally I find the one you use the least readable because of all the
concatenations and the escaping. Even with syntax highlighting it still
looks ugly to me. SGML/XML also allow single quotes around attribute
values, so some possible alternatives would be for example:

$xml .= '<?xml version="1.0" encoding="UTF-8"?>'.CRLF;

or simply

$xml .= "<?xml version='1.0' encoding='UTF-8'?>\n";

sprintf() can also be very helpful for more complex strings with many
embedded variables or expressions.

3) You could also have a look at the DOM extension to create your XML.

Just my 3 cents.

Micha
Reply With Quote
  #6 (permalink)  
Old 12-14-2007
Dikkie Dik
 
Posts: n/a
Default Re: XML encoding

> 1) Is there any specific need for a CRLF line break? Usually a LF ("\n")
> should be more than enough. You could also define your line break as a
> constant, which is what I usually prefer in such cases:


If I recall correctly, End-Of-Line-characters are always LineFeeds in
xml. It is in the XML standard.


Best regards
Reply With Quote
  #7 (permalink)  
Old 12-14-2007
Michael Fesser
 
Posts: n/a
Default Re: XML encoding

..oO(Dikkie Dik)

>> 1) Is there any specific need for a CRLF line break? Usually a LF ("\n")
>> should be more than enough. You could also define your line break as a
>> constant, which is what I usually prefer in such cases:

>
>If I recall correctly, End-Of-Line-characters are always LineFeeds in
>xml. It is in the XML standard.


Yes, it's how an XML processor is supposed to handle the line breaks,
even if they're written as CRLF.

2.11 End-of-Line Handling
http://www.w3.org/TR/xml/#sec-line-ends

But of course you could also write all your XML without any line breaks
on a single line.

Micha
Reply With Quote
  #8 (permalink)  
Old 12-14-2007
Matthew
 
Posts: n/a
Default Re: XML encoding

Thanks for the info and hints. I'm new to PHP so your comments are helpful.

> Just some additional "style hints":
>
> 1) Is there any specific need for a CRLF line break? Usually a LF ("\n")
> should be more than enough. You could also define your line break as a
> constant, which is what I usually prefer in such cases:
>
> define('CRLF', "\r\n");


I've been using something similar to define '<br />' but didn't bother
making a define for one function's benefit.


> 2) There are many ways to write an XML string with embedded variables.
> Personally I find the one you use the least readable because of all the
> concatenations and the escaping. Even with syntax highlighting it still
> looks ugly to me. SGML/XML also allow single quotes around attribute
> values, so some possible alternatives would be for example:
>
> $xml .= '<?xml version="1.0" encoding="UTF-8"?>'.CRLF;
>
> or simply
>
> $xml .= "<?xml version='1.0' encoding='UTF-8'?>\n";


Both are much clearer than mine, I'll use the second and modify my code.
Didn't know there was a .= operator, useful.


> sprintf() can also be very helpful for more complex strings with many
> embedded variables or expressions.


I remember that from my C days, many moons ago.


> 3) You could also have a look at the DOM extension to create your XML.


Will do.

Thanks a lot.
Reply With Quote
  #9 (permalink)  
Old 12-14-2007
Matthew
 
Posts: n/a
Default Re: XML encoding

Dikkie Dik emailed this:
>> 1) Is there any specific need for a CRLF line break? Usually a LF ("\n")
>> should be more than enough. You could also define your line break as a
>> constant, which is what I usually prefer in such cases:

>
> If I recall correctly, End-Of-Line-characters are always LineFeeds in
> xml. It is in the XML standard.


Thanks Dikkie.
Reply With Quote
  #10 (permalink)  
Old 12-14-2007
Matthew
 
Posts: n/a
Default Re: XML encoding

Michael Fesser emailed this:
> .oO(Dikkie Dik)
>
>>> 1) Is there any specific need for a CRLF line break? Usually a LF ("\n")
>>> should be more than enough. You could also define your line break as a
>>> constant, which is what I usually prefer in such cases:

>> If I recall correctly, End-Of-Line-characters are always LineFeeds in
>> xml. It is in the XML standard.

>
> Yes, it's how an XML processor is supposed to handle the line breaks,
> even if they're written as CRLF.
>
> 2.11 End-of-Line Handling
> http://www.w3.org/TR/xml/#sec-line-ends
>
> But of course you could also write all your XML without any line breaks
> on a single line.


But not very human readable. Hence my line breaks.

Cheers again.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:17 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0