Background

I was having some problems with double escaping when creating XML via PHP5 SimpleXML library. Turns out I had stumbled upon, (IMHO at least) an inconsistency in how SimpleXML handles escaping.

When generating XML, consideration needs to made as to how escaping of the reserved characters  < > & ” ‘ are to be handled (Although the apostrophe is an interesting case by itself :) ). The problem I was having is, I did not realise that, depending on how you use SimpleXML, the library will choose to automatically escape your text or require that you do the escaping. In the latter case, if you had already done the escaping, it results in double escaping. The real issue is there seems to be that this behavior is not documented at all. That coupled with the fact that there have been bugs in PHP versions prior to 5.2.6 (#4478) around this module makes this whole issue utterly confusing. I hope to help document what I have found to help the traveler that follows :-) .

A lot of the information that follows was a result of reading the following link : http://bugs.php.net/bug.php?id=45253

SimpleXML dual API

The first thing that needs to be understood clearly is that there are 2 ways to create text nodes and attributes in SimpleXML. I am going to refer to them as magic and non-magic methods.

Creating a text node

Non magic:

$sxml=new SimpleXMLElement(’<test></test>’);
$sxml->addChild(’child1′,’One & Two’);

Magic:

$sxml=new SimpleXMLElement(’<test></test>’);
$sxml->child3 = ‘One & Two’;

Creating an attribute

Non magic:

$sxml=new SimpleXMLElement(’<test></test>’);
$sxml->addAttribute(’child1′,’One & Two’);

Magic:

$sxml=new SimpleXMLElement(’<test></test>’);
$sxml['child3'] = ‘One & Two’;

The dual nature API, although cool, can lead to confusion as the automatic escaping rules are different, not only in the magic and non-magic use case but also whether you are creating text nodes or attributes!

Finding

When it comes to attributes , never escape the attributes (whether you are using the magic or non magic methods), SimpleXML will do it for you. When it comes to setting text nodes, if you are using the magic methods, don’t escape, otherwise if you’re setting the value from the addChild method directly, you will have to escape. The way I have chosen to do it is always use the magic methods (for both attribute and text nodes), and let SimpleXML handle the escaping. The test script which I used is here. Tested in PHP 5.2.6 .

Apostrophe

Turns out that the apostrophe character is not escaped automatically. The only explanation I can find is here, where it points to libxml as the reason behind this.

One Response to “PHP SimpleXML escaping, here be dragons”
  1. Pie says:

    I hate you

Leave a Reply

hair loss home remedy doxycycline 100mg clonazepam withdrawal where to buy condoms herbs for breast growth viagra levitra cialis mestinon cialis 30 oral cheap zocor viagra online usa all natural antibiotics cheap cialis tadalafil order viagra online in germany lowest price generic viagra fda levitra viagra online prescription bacterial diarrhea malaria therapy dog products online new diabetes drug buy now cialis new cholesterol medicines calcium channel blocker hypertension medicine online woman hair loss remedy sildenafil kamagra blood clot symptoms pharmacy viagra celebrex generic viagra buy entocort naturally increase breast size floxin generic for lipitor celecoxib 200mg famvir dose killing parasites buy ultram where acne medication online cialis coupon excersises to increase bust online pharmacy viagra cheapest levitra treatment for itching gonorrhea cure medications to help clot blood ativan without prescription zyrtec online buy levitra cheapest celebrex prednisone buy us pharmacy order erectile dysfunction medications generic for nexium online allegra viagra on line cialis buy online zoloft drug levitra side affects online cozaar doxycycline dose will levitra help buy cheap generic cialis prostate cancer support buy pills without a prescription i need viagra today best hair loss treatment metoclopramide fluconazole buy viagra online diazapan is valium order vermox tablets dog ear problem sildenafil online cialis viagra on line online order viagra how to avoid a hangover valtrex dosage albuterol proventil xanax 5mg viagra for sex cialis levitra viagra generic for zocor cialis 5mg tablets buy celexa quitting zyban cialis 10 20 mg cialis 5mg tablets prednisone 10mg increase libido women genaric levitra levitra prescription online stores hair loss products on line drug store prozac antidepressants clomid no prescription nolvadex 20mg buy viagra online cheap zyprexa 5mg cheep daily cialis diflucan dosage cialis buy online cialis 50mg cold v flu viagra buy online cat anxiety medicine how to treat cold sores no prescription antibiotics overnight delivery florida sildenafil dosage buy cheap cialis online cialis cheapest online prices buy cialis online uk coumadin information medication ranitidine medications celebrex buy cheap viagra online buying cialis online genaric viagra generic for lipitor online sildenafil viagra online overnight medical treatment for insomnia prozac on line tetracycline for dogs skin infection treatment how effective is plan b cat care products online cialis new hair loss treatmen buy levitra online cheap cialis sale online motilium buy buy premarin on line gout medicine price of levitra cat urinary tract disease cheapest generic cialis online sexual performance us levitra viagra online without prescription usa orgasm enhancers how to reduce high blood pressure hard stools discount viagra online celebrex information bactroban urinary tract health cialis on line where can i buy arthritis drugs pain med without a prescription viagra to buy