[prev in list] [next in list] [prev in thread] [next in thread] 

List:       php-general
Subject:    Re: [PHP] Parsing entities in XML attributes
From:       Decapode Azur <blue_prawn () tuxfamily ! org>
Date:       2004-06-24 20:07:26
Message-ID: 200406242202.33868.blue_prawn () tuxfamily ! org
[Download RAW message or body]

Hi,

The problem I had has not exactly the same, but quite close,
it was to make a pure xml template system both data and template,
anyway it was due to the same expat design problem, which is
the entities that are located in attribute values inside tags.

The solution I have used was just to preserve expat from parsing entities,
and only need 2 additionnal lines in the script (3 with the constant define;).

Just after the input replace the char '&' with something else,
and just before the output put it back :

/** Preserve entities from removal */
define('ENTITY_PRESERVE', '£$¥¤');

[...]

#replacement at input :

        while ($data = fread($fp, 4096)) {
            $data = str_replace('&', ENTITY_PRESERVE, $data);  // HERE
            if (!xml_parse($this->_parser, $data, feof($fp))) {
                die(sprintf("XML error: %s at ligne %d",
                            xml_error_string(xml_get_error_code($xml_parser)),
                            xml_get_current_line_number($xml_parser)));
            }
        }

[...]

#replacement for output with your code :

function start_tag($parser, $name, $attrs)
{
    global $text;
    if($name == "CUSTOMTEXT") {
        $text = $attrs['TEXT'];
        $text = str_replace(ENTITY_PRESERVE, '&', $text);  // Put '&' back !
    }
    // more tags here ..
}


I am sure that there could be better solutions.
If someone know about one, please email me :)


Cheers
-- 
Gabriel Birke a écrit :
> Hello!
>
> I've got an XML file that has the following tag:
> <customtext text="first line&lt;br /&gt;second line" />
>
> I parse it with the following handler (simplified a bit):
>
> function start_tag($parser, $name, $attrs)
> {
> 	global $text;
> 	if($name == "CUSTOMTEXT")
> 		$text = $attrs['TEXT'];
> 	// more tags here ..
> }
>
> When I echo $text in a HTML page, I see a linebreak - just as I
> intended.
>
> One of our customers has the script installed on his server and he
> doesn't see a linebreak but sees <br /> printed out. He has to write
> the tag like this:
> <customtext text="first line <br />second line" />
>
> Now I've got two questions:
> 1. Which tag is correct XML?
> 2. What is the cause of this?
>
> I'm running the script with PHP 4.1.2 and EXPAT Version 1.95.2
> Don't know what our customer has running.
>
> With best regards
>
> Gabriel Birke

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic