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'开发者_StackOverflowm using:
$source = 'stockdata.xml';
$stock = simplexml_load_file($source);
foreach($stock->StockItem as $item)
{
........
}
You need to use XML (and HTML) entities. Instead of Milk & Cheese
use Milk & Cheese
. You can find a complete list at W3Schools.
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
精彩评论