xml with curly quotes?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rssajkow
Forum Newbie
Posts: 11
Joined: Mon Oct 02, 2006 1:03 am

xml with curly quotes?

Post by rssajkow »

twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Trying to figure out a way to parse XML using simplexml_load_file and curly quotes/apostrophes can anyone help?

example:

xml:
[syntax="xml"]
<knife>
<maker>Dan Burke</maker>
<type>Folder</type>
<named>no</named>
<price>599</price>
<shipping>2</shipping> 
<description>
Dan Burke Gentleman&rsquo;s Folder Dan's work speaks for itself. Just a perfect carry size, wonderful fit and finish and truly remarkable Scales backed by Gold Foil.
</description>
<thumb>./images/thm_burkeFolder.jpg</thumb>
<image1>./images/burkeFolder.jpg</image1>
<image2></image2>
<image3></image3>
<image4></image4>
<image5></image5>
<image6></image6>
<featured>no</featured>
<reduced>no</reduced>
</knife>
php product page:[/syntax]

Code: Select all

<?php
$xml = simplexml_load_file('knives.xml');


$per_row = 6;
echo '
<table cellpadding="0" border="0" cellspacing="0">';
$count = 0;
for($x = 0; $x < count($xml); $x++)
{
if($count == 0)
{
echo '
<tr>';
}

foreach($xml -> knife as $knife){
if($knife->price < 1000 && $knife->price > 1){
$type=stripslashes($knife->type);

echo "<td class=\"product\" align=\"center\" valign=\"bottom\" height=\"116\">

<div height=\"116\">
<a href=\"detail.php?image1=$knife->image1\" target=\"_self\">

 <img src=\"$knife->thumb\" alt=\"$knife->maker custom knife\">
 </a>
 
 </div>
 
 <br><span class=\"maker\">$knife->maker</span><br>";
 
 if($knife->named=="no"){
 echo"$type";
 }else{
 echo "&ldquo;$type&rdquo;";}
 
 echo"
 
 <br>$$knife->price.
 <br><br>
 <a href=\"http://ww8.aitsafe.com/cf/add.cfm?userid=something&product=$knife->maker $knife->type&price=$knife->price&units=$knife->shipping&return=www.exquisiteknives.com\" class=\"button\" target=\"_blank\">Add to Cart</a><br><br>

</td>";

$count++;
if(($count == $per_row) || ($xml[$x] == end($xml)))

{
echo '</tr>';
$count = 0;
}
}
}
echo '
</table>';
exit;
}





?>
this script yields the following errors unless I replace the &rsquo in <description> with a standard straight apostrophe ('). For some reason it doesnt mind using & which will always display as & when placed in the XML <description>

Warning: knives.xml:189: parser error : Entity 'rsquo' not defined in C:\Program Files\Apache Group\Apache2\htdocs\web_knives\prod1.php on line 13

Warning: Dan Burke Gentleman’s Folder Dan's work speaks for itself. Just a perfect in C:\Program Files\Apache Group\Apache2\htdocs\web_knives\prod1.php on line 13

Warning: ^ in C:\Program Files\Apache Group\Apache2\htdocs\web_knives\prod1.php on line 13

Warning: Invalid argument supplied for foreach() in C:\Program Files\Apache Group\Apache2\htdocs\web_knives\prod1.php on line 28


twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

There are only five built-in entities in xml 1.0.
http://www.w3.org/TR/2006/REC-xml-20060816/#sec-predefined-ent wrote:<!ENTITY lt "<">
<!ENTITY gt ">">
<!ENTITY amp "&">
<!ENTITY apos "'">
<!ENTITY quot """>
all other entities must be declared before using. E.g. by using a matching DOCTYPE.
&rquot is a html4 entity. Browsers include the html doctype definitions (including the entity declarations) by default. xml parsers don't (at least I don't know any that do ;)).
What entities are used for what purpose?
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

There are some thorny issues with resolving entities like this, namely you really don't want the system to running off to W3C's server to grab a copy of the entity file. See http://www.whump.com/moreLikeThis/link/03815 for more details.

If given the choice, I would write the curly quote directly down as a UTF-8 character, however, some text editors make that difficult.
Post Reply