terrid25
terrid25

Reputation: 1946

simpleXML parsing

I'm trying to parse some XML using simpleXML in PHP.

The problem I'm having though, is that some nodes have values like Milk & Cheese and I'm getting a parse error.

What's the easiest way of parsing XML using simpleXML with chars such as & and / being present?

Thanks

EDIT:

I'm using:

 $source = 'stockdata.xml';
 $stock =  simplexml_load_file($source);

foreach($stock->StockItem as $item)
{
 ........
}

Upvotes: 1

Views: 569

Answers (2)

Bailey Parker
Bailey Parker

Reputation: 15905

You need to use XML (and HTML) entities. Instead of Milk & Cheese use Milk & Cheese. You can find a complete list at W3Schools.

Upvotes: 3

pleasedontbelong
pleasedontbelong

Reputation: 20102

quick solution:

$xml = str_replace('&','&',$xml); //$xml is the content in string format

a better solution: [ if you control the xml ]

add CDATA in the xml tags

<name><![CDATA[ Milk & honey]]></script> 

and you'll need to tell SimpleXml to take care of the CDATA:

$sxml = simplexml_load_file('myfile.xml','SimpleXMLElement', LIBXML_NOCDATA);

Hope this helps

Upvotes: 2

Related Questions