shanejones Posted May 24, 2011 Share Posted May 24, 2011 I am trying to parse an XML file and there are some values that contain a : as you can see below. $rss_xml = SimpleXML_Load_String($rss_data); foreach ($rss_xml->channel->item as $item) { echo $item->title; echo "<br />"; echo $item->link; echo "<br />"; echo $item->description; echo "<br />"; echo $item->g:price; echo "<br />"; echo $item->g:image_link; echo "<hr />"; } For some reason because of this : I get the error. HP Parse error: syntax error, unexpected ':', expecting ',' or ';' in /home/blah/blah/blah/index.php I have tried escaping it with a \ and also quoting the last element in the echo but it still will not have it. Any suggestions. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/237331-xml-parsing-issue/ Share on other sites More sharing options...
RussellReal Posted May 24, 2011 Share Posted May 24, 2011 I am trying to parse an XML file and there are some values that contain a : as you can see below. $rss_xml = SimpleXML_Load_String($rss_data); foreach ($rss_xml->channel->item as $item) { echo $item->title; echo "<br />"; echo $item->link; echo "<br />"; echo $item->description; echo "<br />"; echo $item->g:price; echo "<br />"; echo $item->g:image_link; echo "<hr />"; } For some reason because of this : I get the error. HP Parse error: syntax error, unexpected ':', expecting ',' or ';' in /home/blah/blah/blah/index.php I have tried escaping it with a \ and also quoting the last element in the echo but it still will not have it. Any suggestions. Thanks you're probably trying to use the :: operator instead of ':', however, that will not work I don't believe, replace your ':' with '->' and see where that gets you Quote Link to comment https://forums.phpfreaks.com/topic/237331-xml-parsing-issue/#findComment-1219552 Share on other sites More sharing options...
PFMaBiSmAd Posted May 24, 2011 Share Posted May 24, 2011 Should work - echo $item->{'g:price'}; Ref: Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe. Example #3 Getting <line> <?php include 'example.php'; $xml = new SimpleXMLElement($xmlstr); echo $xml->movie->{'great-lines'}->line; Quote Link to comment https://forums.phpfreaks.com/topic/237331-xml-parsing-issue/#findComment-1219556 Share on other sites More sharing options...
shanejones Posted May 24, 2011 Author Share Posted May 24, 2011 Ok so adding the angled bracket and quote stops it from erroring. Now I have an issue with it not pulling the data from the XML file. It now looks like this $rss_xml = SimpleXML_Load_String($rss_data); foreach ($rss_xml->channel->item as $item) { echo $item->title; echo "<br />\n"; echo $item->link; echo "<br />\n"; echo $item->description; echo "<br />\n"; echo $item->{'g:price'}; echo "<br />\n"; echo $item->{'g:image_link'}; echo "<hr />\n\n"; } The XML file is here if you need to see the source. www.manuallinkbuilding.co.uk/index.php?rss=true&action=product_list&xmlformat=google Thanks Quote Link to comment https://forums.phpfreaks.com/topic/237331-xml-parsing-issue/#findComment-1219585 Share on other sites More sharing options...
shanejones Posted May 24, 2011 Author Share Posted May 24, 2011 Solved my issue by using file_get_contents() to put the XML contents into a string and then user preg_replace to remove the g: values. then parsed it as normal. Thanks for you help. Quote Link to comment https://forums.phpfreaks.com/topic/237331-xml-parsing-issue/#findComment-1219761 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.