Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/237331-xml-parsing-issue/
Share on other sites

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 :)

Link to comment
https://forums.phpfreaks.com/topic/237331-xml-parsing-issue/#findComment-1219552
Share on other sites

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;

 

Link to comment
https://forums.phpfreaks.com/topic/237331-xml-parsing-issue/#findComment-1219556
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/237331-xml-parsing-issue/#findComment-1219585
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.