Re: XML. Prevent < from turning into &lt;

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On 9 Sep 2009, at 16:37, Ashley Sheridan wrote:

On Wed, 2009-09-09 at 15:14 +0100, Matthew Croud wrote:
On 9 Sep 2009, at 15:09, Ashley Sheridan wrote:

On Wed, 2009-09-09 at 14:46 +0100, Matthew Croud wrote:
On 9 Sep 2009, at 14:36, Bob McConnell wrote:

From: Bastien Koert
On Wed, Sep 9, 2009 at 5:27 AM, Matthew
Croud<matt@xxxxxxxxxxxxxxxxxx> wrote:

Hiya,
I'm writing an app that let's my client upload images, the image
html code
is added to an XML file.
Take a look at the <image> element below:

<item Code="e1022">
 <code>e1022</code>
 <image><![CDATA[<img src="uploads/image2.jpg" alt="Homepage
Image"
width="124" height="70" />]]></image>
 <name>Blue Ski Trousers</name>
 <price>8.99</price>
</item>

Now, whenever PHP writes this to the XML files, it turns the < and
into
&lt; and &gt; . which means it does not display on the webpage.
How can I
prevent this from happening ?


str_replace?


Does the xml string get passed to htmlentities() or a similar
function before it is sent to the browser? That would explain the
substitutions. I saw an xmlentities() variation mentioned somewhere.

Bob McConnell

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



Hi Bob,

Nope the string doesn't get passed into any function.
Here is the XML:
_________________________________________________________________

<?xml version="1.0" encoding="UTF-8"?>
<clothes>

 <item Code="e1021">
   <code>e1021</code>
   <image><![CDATA[<img src="uploads/image1.jpg" alt="Homepage
Image" width="124" height="70" border="1"
onclick="MM_openBrWindow('uploads/
image1.jpg','Preview','width=680,height=520')" />]]></image>
   <name>Red Jacket</name>
   <desc>An adult sized red jacket</desc>
   <size>adult</size>
   <price>12.99</price>
 </item>

 <item Code="e1022">
   <code>e1022</code>
   <image><![CDATA[<img src="uploads/image2.jpg" alt="Homepage
Image" width="124" height="70" border="1"
onclick="MM_openBrWindow('uploads/
image2.jpg','Preview','width=680,height=520')" />]]></image>
   <name>Blue Ski Trousers</name>
   <desc>A pair of Blue Ski Trousers</desc>
   <size>child</size>
   <price>8.99</price>
 </item>

</clothes>

________________________________________________________________

Now the image tags I have written here I have done manually, but when I use PHP using DOM they come out like this (note the image element):

<item Code="e1024">
<code>e1024</code>
<image>&lt;![CDATA[&lt;img src="uploads/image4.jpg" alt="Homepage
Image" width="124" height="70" border="1"
onclick="MM_openBrWindow('uploads/
image4.jpg','Preview','width=680,height=520')" /&gt;]]&gt;</image>
<name>orange top</name>
<desc>an orange jacket</desc>
<size>large</size>
<price>14.50</price>
</item>




Matthew Croud
Studio

Obvious Print Solutions Limited
Unit 3 Abbeygate Court
Stockett Lane
Maidstone
Kent
ME15 0PP

T | 0845 094 9704
F | 0845 094 9705
www.obviousprint.com





I've not experienced this with using any DOM functions, but if we can
see your code it might help us.

Thanks,
Ash
http://www.ashleysheridan.co.uk




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





Well i'll pop the PHP down here, i'll trim it as much as i can:
________________________________________________________________________

$code = $_POST['code'];
$name = $_POST['name'];
$desc = $_POST['desc'];
$size = $_POST['size'];
$price = $_POST['price'];


#load an XML document into the DOM

$dom = new DomDocument();
$dom -> load("items.xml");

#create elements

$Xitem =  $dom -> createElement("item");
$Xcode =  $dom -> createElement("code");
$Ximage =  $dom -> createElement("image");
$Xname =  $dom -> createElement("name");
$Xdesc = $dom -> createElement("desc");
$Xsize = $dom -> createElement("size");
$Xprice = $dom -> createElement("price");

#create text nodes

$Xcodetext =  $dom -> createTextNode($code);
$Ximagetext =  $dom -> createTextNode( "<![CDATA[<img src=\"uploads/
$UploadName\" alt=\"Homepage Image\" width=\"124\" height=\"70\"
border=\"1\" onclick=\"MM_openBrWindow('uploads/
$UploadName','Preview','width=680,height=520')\" />]]>" );
$Xnametext =  $dom -> createTextNode($name);
$Xdesctext = $dom -> createTextNode($desc);
$Xsizetext =    $dom -> createTextNode($size);
$Xpricetext =    $dom -> createTextNode($price);

#append the text nodes to the inner nested elements

$Xcode  -> appendChild($Xcodetext);
$Ximage  -> appendChild($Ximagetext);
$Xname  -> appendChild($Xnametext);
$Xdesc -> appendChild($Xdesctext);
$Xsize    -> appendChild($Xsizetext);
$Xprice    -> appendChild($Xpricetext);

#append the inner nested elements to the <item> element

$Xitem -> appendChild($Xcode);
$Xitem -> appendChild($Ximage);
$Xitem -> appendChild($Xname);
$Xitem -> appendChild($Xdesc);
$Xitem -> appendChild($Xsize);
$Xitem -> appendChild($Xprice);
$Xitem -> setAttribute("Code",$code);

#append the <title> element to the root element

$dom -> documentElement -> appendChild($Xitem);

#create a new enlarged xml document

$dom -> save("items.xml");

_____________________________________________________
It looks like it's because you are creating an image object by appending
a text node inside CDATA to it. My guess is that a text node will have
the contents automatically made 'safe' for the XML entities which are <,
, &, etc.

Why are you appending the child item like this? What's wrong with adding
it as a proper object, attributes and all?

Thanks,
Ash
http://www.ashleysheridan.co.uk





Hi Ash,

Thanks for getting back to me,
In all honesty this method is what's implemented in the course book i've been following.

I been using XML longer and i've never had this issue using image tags before and getting the output I expected. Is there a way around this safety mechanism ? Otherwise i'll have to think of another way of doing this.

Cheers.

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


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux