开发者

simpleXML parsing

开发者 https://www.devze.com 2023-04-03 04:38 出处:网络
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.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消