xml entity problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jos van Uden

    xml entity problem

    Can somebody explain why the following file
    has the wrong output:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <test>
    <elem>‘bla bla bla’</elem>
    </test>

    Expected: ‘bla bla bla’
    output: ?bla bla bla?

    It's not caused by the browser, but
    by the xml parser.

    Thanks.


    test script:


    <?php

    $file = "test.xml";
    $testdata;
    $tagname;

    function startElement($p arser, $name, $attrs) {
    global $tagname;
    $tagname = $name;
    }

    function endElement($par ser, $name) {
    }

    function characterData(& $parser, $data) {
    global $testdata, $tagname;
    if(trim($data) != "") {
    switch($tagname ) {
    case 'ELEM' :
    $testdata .= $data;
    break;
    }
    }
    }

    $xml_parser = xml_parser_crea te();
    xml_set_element _handler($xml_p arser, "startEleme nt", "endElement ");
    xml_set_charact er_data_handler ($xml_parser, 'characterData' );
    if (!($fp = fopen($file, "r"))) {
    die("could not open XML input");
    }

    while ($data = fread($fp, 4096)) {
    if (!xml_parse($xm l_parser, $data, feof($fp))) {
    die(sprintf("XM L error: %s at line %d",
    xml_error_strin g(xml_get_error _code($xml_pars er)),
    xml_get_current _line_number($x ml_parser)));
    }
    }
    xml_parser_free ($xml_parser);

    print "output : " .$testdata;


    ?>
  • Aphrael

    #2
    Re: xml entity problem

    Jos van Uden wrote:
    [color=blue]
    > Can somebody explain why the following file
    > has the wrong output:
    >
    > <?xml version="1.0" encoding="iso-8859-1"?>
    > <test>
    > <elem>‘bla bla bla’</elem>
    > </test>
    >
    > Expected: ‘bla bla bla’
    > output: ?bla bla bla?
    >
    > It's not caused by the browser, but
    > by the xml parser.
    >
    > Thanks.
    >
    >
    > test script:
    >
    >
    > <?php
    >
    > $file = "test.xml";
    > $testdata;
    > $tagname;
    >
    > function startElement($p arser, $name, $attrs) {
    > global $tagname;
    > $tagname = $name;
    > }
    >
    > function endElement($par ser, $name) {
    > }
    >
    > function characterData(& $parser, $data) {
    > global $testdata, $tagname;
    > if(trim($data) != "") {
    > switch($tagname ) {
    > case 'ELEM' :
    > $testdata .= $data;
    > break;
    > }
    > }
    > }
    >
    > $xml_parser = xml_parser_crea te();
    > xml_set_element _handler($xml_p arser, "startEleme nt", "endElement ");
    > xml_set_charact er_data_handler ($xml_parser, 'characterData' );
    > if (!($fp = fopen($file, "r"))) {
    > die("could not open XML input");
    > }
    >
    > while ($data = fread($fp, 4096)) {
    > if (!xml_parse($xm l_parser, $data, feof($fp))) {
    > die(sprintf("XM L error: %s at line %d",
    > xml_error_strin g(xml_get_error _code($xml_pars er)),
    > xml_get_current _line_number($x ml_parser)));
    > }
    > }
    > xml_parser_free ($xml_parser);
    >
    > print "output : " .$testdata;
    >
    >
    > ?>[/color]
    _I know_ this sounds silly. But try
    <?php echo '<'.'?xml version="1.0" encoding="iso-8859-1"?'.'>'; ?>

    (that's one thing that feedvalidator drove me to do... ^^; Still having
    problems, though, so if there is a specialist of RSS feed here, I'll be
    happy to have help ;-) )

    Aphrael...
    --
    "La demande mondiale d’ordinateurs n’excédera pas cinq machines."
    (Thomas Watson, Fondateur d'IBM, 1945)

    Comment

    Working...