Jump to content

Html entities inside attribute values of an xml?


kutchbhi

Recommended Posts

I am looking at an xml that has the greater than sign inside the attribute value.

 

<CATEGORY id='163' name='Toys > Other '></CATEGORY>

 

My job is to create a similar xml where the > sign exists inside the node's attribute value ? is this possible ? how ?

 

I tried this:

 

$catName = htmlspecialchars_decode(str_replace('-', '>',$CategoryName )) ;

 

but it just encoded the > into > .

 

Thanks

Are you sure it's an issue?

$str = <<<XML
<A>
<Category name='Toys > Other'>
<Item>item 1</Item>
<Item>item 2</Item>
<Item>item 3</Item>
</Category>
<Category name='Games > Other'>
<Item>item 4</Item>
<Item>item 5</Item>
<Item>item 6</Item>
</Category>
</A>
XML;

$xml = simplexml_load_string($str);

foreach ($xml->Category as $cat) {
    echo "{$cat['name']}<br>";
    foreach ($cat->Item as $i) {
        echo " • $i<br>";
    }
}

/********** OUTPUT **********

Toys > Other
• item 1
• item 2
• item 3
Games > Other
• item 4
• item 5
• item 6

*******************************/

When I do like this  

$category = $dom->createElement('CATEGORY') ;
$catName = htmlspecialchars_decode(str_replace('-', '>',$CategoryName )) ;
$category->setAttribute('name', $catName  );

I get a > instead of a > . am I doing something wrong ?

 

also right now I am just using a str_replace on the whole file that works..but not sure if its ok.. 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.