0

Possible Duplicate:
PHP library for parsing XML with a colons in tag names?

I have the xml shown below and I want to parse out the product title. When I use the php code below, I get "Parse error: syntax error, unexpected ':' in /home/content/c/a/s/cashme/html/buylooper/xml.php on line 5" because of the ":" located in the tag. How do I resolve this?

*update: I've got the answer to the first part, but am having trouble in how to parse out an attribute of an xml tag. The tag I am having trouble with is the "s:image" tag (link attribute) inside the "s:images" tag.

    <?php
    $url = 'xml-file.xml';
    $xml = simplexml_load_file($url);
    $title = $xml->entry[0]->s:product->s:title; 
    //print
    echo '<br/>';
    echo $title;
    ?>


  <entry gd:kind="shopping#product"> 
  <s:product> 
   <s:googleId>9400569674928563633</s:googleId> 
   <s:author> 
    <s:name>Amazon.com</s:name> 
    <s:accountId>2860562</s:accountId> 
   </s:author> 
   <s:creationTime>2010-08-19T05:50:21.000Z</s:creationTime> 
   <s:modificationTime>2012-01-26T23:54:26.000Z</s:modificationTime> 
   <s:country>US</s:country> 
   <s:language>en</s:language> 
   <s:title>Canon powershot s95 10 mp digital camera with 3.8x wide angle optical image stabilized zoom and 3.0-inch lcd</s:title> 
   <s:description>desc</s:description> 
   <s:link>http://www.amazon.com/Canon-PowerShot-S95-Stabilized-3-0-Inch/dp/B003ZSHNGS</s:link> 
   <s:brand>Canon</s:brand> 
   <s:condition>new</s:condition> 
   <s:gtin>00013803126556</s:gtin> 
   <s:gtins> 
    <s:gtin>00013803126556</s:gtin> 
   </s:gtins> 
   <s:inventories> 
    <s:inventory channel="online" availability="inStock"> 
     <s:price shipping="0.0" currency="USD">340.41</s:price> 
    </s:inventory> 
   </s:inventories> 
   <s:images> 
    <s:image link="http://ecx.images-amazon.com/images/I/519z3AjKzHL._SL500_AA300_.jpg"/> 
   </s:images> 
  </s:product> 
 </entry> 
Community
  • 1
  • 1
sharataka
  • 5,014
  • 20
  • 65
  • 125
  • 2
    I think this is the same? http://stackoverflow.com/questions/1575788/php-library-for-parsing-xml-with-a-colons-in-tag-names – pjumble Jan 28 '12 at 22:29

2 Answers2

0

You need to get the correct namespace with . This is untested, but might do the trick:

$url = 'xml-file.xml';
$xml = simplexml_load_file($url);
$namespaces = $xml->entry->getNameSpaces(true);

// Get children of the correct namespace
$s = $xml->entry[0]->children($namespaces['s']);
$title = $s->product->title;

//print
echo '<br/>';
echo $title;
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
  • This is working for each of them except for s:images. I think it's because there's no closing tag for "s:image" inside of "s:images". Link is an attribute; how do I parse the attribute? – sharataka Jan 28 '12 at 23:19
0

Parse the namespaces first.

$namespaces = $xml->getNameSpaces(true);
$s = $xml->children($namespaces['s']);
echo (string)$s->product->title. "\n";
echo (string)$s->product->images->image->attributes()->link;
Shiplu Mokaddim
  • 56,364
  • 17
  • 141
  • 187
  • This is working for each of them except for s:images. I think it's because there's no closing tag for "s:image" inside of "s:images". Link is an attribute; how do I parse the attribute? – sharataka Jan 28 '12 at 23:20
  • @sharataka `s:images` will not be shown as there is no text in there. Parse `link` attribute by `$s->product->images->image->attributes()->link`. See my update. – Shiplu Mokaddim Jan 28 '12 at 23:25