Saving XML file with iso-8859-1 encoding fails

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Börni

    Saving XML file with iso-8859-1 encoding fails

    Hi,
    The question might sound (and probably is;-) stupid, but why doesnt the
    follwing code work?? Is there something i dont see?

    $doc = new DOMDocument('1. 0', 'iso-8859-1');
    $doc->formatOutput = true;

    $root = $doc->createElement( 'root');
    $root = $doc->appendChild($r oot);

    $head = $doc->createElement( 'head');
    $head = $root->appendChild($h ead);

    $title = $doc->createElement( 'title');
    $title = $head->appendChild($t itle);

    /* probably the real sign gets killed,
    so here is the html: ä (an umlaut) */
    $text = $doc->createTextNode ('ä');
    $text = $title->appendChild($t ext);


    echo $doc->saveXML();

    Thanks in advance
  • Börni

    #2
    Re: Saving XML file with iso-8859-1 encoding fails

    Börni wrote:[color=blue]
    > Hi,
    > The question might sound (and probably is;-) stupid, but why doesnt the
    > follwing code work?? Is there something i dont see?
    >
    > $doc = new DOMDocument('1. 0', 'iso-8859-1');
    > $doc->formatOutput = true;
    >
    > $root = $doc->createElement( 'root');
    > $root = $doc->appendChild($r oot);
    >
    > $head = $doc->createElement( 'head');
    > $head = $root->appendChild($h ead);
    >
    > $title = $doc->createElement( 'title');
    > $title = $head->appendChild($t itle);
    >
    > /* probably the real sign gets killed,
    > so here is the html: ä (an umlaut) */
    > $text = $doc->createTextNode ('ä');
    > $text = $title->appendChild($t ext);
    >
    >
    > echo $doc->saveXML();
    >
    > Thanks in advance[/color]

    Sorry i forgot to post the error!

    Warning: output conversion failed due to conv error in /websites/dom.php
    on line 22

    Warning: Bytes: 0xE4 0x3C 0x2F 0x74 in /websites/dom.php on line 22

    Comment

    • Andy Hassall

      #3
      Re: Saving XML file with iso-8859-1 encoding fails

      On Mon, 07 Feb 2005 20:19:05 +0000, Börni <b.reiter@onlin ehome.de> wrote:
      [color=blue]
      >Hi,
      >The question might sound (and probably is;-) stupid, but why doesnt the
      >follwing code work?? Is there something i dont see?
      >
      >$doc = new DOMDocument('1. 0', 'iso-8859-1');
      >$doc->formatOutput = true;
      >
      >$root = $doc->createElement( 'root');
      >$root = $doc->appendChild($r oot);
      >
      >$head = $doc->createElement( 'head');
      >$head = $root->appendChild($h ead);
      >
      >$title = $doc->createElement( 'title');
      >$title = $head->appendChild($t itle);
      >
      >/* probably the real sign gets killed,
      > so here is the html: &auml; (an umlaut) */
      >$text = $doc->createTextNode ('ä');
      >$text = $title->appendChild($t ext);
      >
      >
      >echo $doc->saveXML();
      >
      >Thanks in advance[/color]

      The user notes on:



      claim:

      "all text methods in domxml expect utf-8 encoded strings as input."

      ... and recommend using utf8_eecode() on the value.

      --
      Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
      <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

      Comment

      • Börni

        #4
        Re: Saving XML file with iso-8859-1 encoding fails

        Andy Hassall wrote:[color=blue]
        > On Mon, 07 Feb 2005 20:19:05 +0000, Börni <b.reiter@onlin ehome.de> wrote:
        >
        >[color=green]
        >>Hi,
        >>The question might sound (and probably is;-) stupid, but why doesnt the
        >>follwing code work?? Is there something i dont see?
        >>
        >>$doc = new DOMDocument('1. 0', 'iso-8859-1');
        >>$doc->formatOutput = true;
        >>
        >>$root = $doc->createElement( 'root');
        >>$root = $doc->appendChild($r oot);
        >>
        >>$head = $doc->createElement( 'head');
        >>$head = $root->appendChild($h ead);
        >>
        >>$title = $doc->createElement( 'title');
        >>$title = $head->appendChild($t itle);
        >>
        >>/* probably the real sign gets killed,
        >> so here is the html: &auml; (an umlaut) */
        >>$text = $doc->createTextNode ('ä');
        >>$text = $title->appendChild($t ext);
        >>
        >>
        >>echo $doc->saveXML();
        >>
        >>Thanks in advance[/color]
        >
        >
        > The user notes on:
        >
        > http://uk2.php.net/manual/en/functio...-text-node.php
        >
        > claim:
        >
        > "all text methods in domxml expect utf-8 encoded strings as input."
        >
        > ... and recommend using utf8_eecode() on the value.
        >[/color]
        Ok, but why then can i specifiy the encoding for the file?!

        Comment

        • Andy Hassall

          #5
          Re: Saving XML file with iso-8859-1 encoding fails

          On Mon, 07 Feb 2005 21:48:59 +0000, Börni <b.reiter@onlin ehome.de> wrote:
          [color=blue]
          >Andy Hassall wrote:[color=green]
          >> On Mon, 07 Feb 2005 20:19:05 +0000, Börni <b.reiter@onlin ehome.de> wrote:
          >>
          >> The user notes on:
          >>
          >> http://uk2.php.net/manual/en/functio...-text-node.php
          >>
          >> claim:
          >>
          >> "all text methods in domxml expect utf-8 encoded strings as input."
          >>
          >> ... and recommend using utf8_eecode() on the value.
          >>[/color]
          >Ok, but why then can i specifiy the encoding for the file?![/color]

          You apparently need to pass all text into the functions as utf8 - and only
          then when the XML is finally output is it re-encoded into the encoding you
          specified.

          Consider:

          andyh@server:~/public_html$ cat test.php
          <?php
          $doc = new DOMDocument('1. 0', 'iso-8859-1');
          $doc->formatOutput = true;

          $root = $doc->createElement( 'root');
          $root = $doc->appendChild($r oot);

          $head = $doc->createElement( 'head');
          $head = $root->appendChild($h ead);

          $title = $doc->createElement( 'title');
          $title = $head->appendChild($t itle);

          /* probably the real sign gets killed,
          so here is the html: &auml; (an umlaut) */
          $text = $doc->createTextNode (utf8_encode('ä '));
          $text = $title->appendChild($t ext);

          echo $doc->saveXML();
          ?>
          andyh@server:~/public_html$ php -q test.php | hexdump -C
          00000000 3c 3f 78 6d 6c 20 76 65 72 73 69 6f 6e 3d 22 31 |<?xml version="1|
          00000010 2e 30 22 20 65 6e 63 6f 64 69 6e 67 3d 22 69 73 |.0" encoding="is|
          00000020 6f 2d 38 38 35 39 2d 31 22 3f 3e 0a 3c 72 6f 6f |o-8859-1"?>.<roo|
          00000030 74 3e 0a 20 20 3c 68 65 61 64 3e 0a 20 20 20 20 |t>. <head>. |
          00000040 3c 74 69 74 6c 65 3e e4 3c 2f 74 69 74 6c 65 3e |<title>ä</title>|
          00000050 0a 20 20 3c 2f 68 65 61 64 3e 0a 3c 2f 72 6f 6f |. </head>.</roo|
          00000060 74 3e 0a |t>.|
          00000063

          Note that the ä has indeed come out in iso-8859-1, i.e a single byte, and not
          in utf8.

          --
          Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
          <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

          Comment

          • Manuel Lemos

            #6
            Re: Saving XML file with iso-8859-1 encoding fails

            Hello,

            on 02/07/2005 06:19 PM Börni said the following:[color=blue]
            > Hi,
            > The question might sound (and probably is;-) stupid, but why doesnt the
            > follwing code work?? Is there something i dont see?
            >
            > $doc = new DOMDocument('1. 0', 'iso-8859-1');
            > $doc->formatOutput = true;
            >
            > $root = $doc->createElement( 'root');
            > $root = $doc->appendChild($r oot);
            >
            > $head = $doc->createElement( 'head');
            > $head = $root->appendChild($h ead);
            >
            > $title = $doc->createElement( 'title');
            > $title = $head->appendChild($t itle);
            >
            > /* probably the real sign gets killed,
            > so here is the html: &auml; (an umlaut) */
            > $text = $doc->createTextNode ('ä');
            > $text = $title->appendChild($t ext);
            >
            >
            > echo $doc->saveXML();
            >
            > Thanks in advance[/color]

            You may want to try this class XML writer class that lets you specify
            the input and output encoding either as ISO-8859-1 or UTF-8. It does not
            require any special extension.



            --

            Regards,
            Manuel Lemos

            PHP Classes - Free ready to use OOP components written in PHP
            Free PHP Classes and Objects 2025 Versions with PHP Example Scripts, PHP Tutorials, Download PHP Scripts, PHP articles, Remote PHP Jobs, Hire PHP Developers, PHP Book Reviews, PHP Language OOP Materials


            PHP Reviews - Reviews of PHP books and other products


            Metastorage - Data object relational mapping layer generator

            Comment

            • Bernhard

              #7
              Re: Saving XML file with iso-8859-1 encoding fails

              Manuel Lemos wrote:[color=blue]
              > Hello,
              >
              > You may want to try this class XML writer class that lets you specify
              > the input and output encoding either as ISO-8859-1 or UTF-8. It does not
              > require any special extension.
              >
              > http://www.phpclasses.org/xmlwriter
              >[/color]

              Thanks, i will look closer at that class!

              Comment

              Working...