I have a node name like:

product:shoes

when i cast it:

echo $xml->channel->item[0]->product:shoes;

i get an error, unexpected ':'

what todo?

    Only work-around I've come up with:

    $var = "product:shoes";
    echo $xml->channel->item[0]->$var;
    

      doesnt seem to be working:

      <rss>
      <channel>
      <item>
        <product:price xmlns:domain="http://www.domain.com/">$16.99</product:price>
      </item>
      </channel>
      </rss>
      
      $price = 'product:price';
      
      echo "Price: ". $xml->channel->item->$price;
      

        Can you show us your complete code?

        I also hope that the XML code snippet you posted is only a portion of the entire code (e.g. a properly formatted XML document). Can you perhaps show us the rest of this XML document?

          adapted my top code to fit the actual xml document:

          <?php
          $xml = simplexml_load_file("http://www.woot.com/salerss.aspx");
          
          echo "WootOff: ". $xml->channel->item[0]->{'woot:wootoff'} . "<br/>";
          echo "WootSold: ". $xml->channel->item[0]->{'woot:soldout'} . "<br/>";
          echo "WootQty: ". $xml->channel->item[0]->{'woot:products'}->{'woot:product'} . "<br/>";
          echo "WootProduct: ". $xml->channel->item[0]->{'woot:product'} . "<br/>";
          echo "WootCondition: ". $xml->channel->item[0]->{'woot:condition'} . "<br/>";
          echo "WootPrice: ". $xml->channel->item[0]->{'woot:price'} . "<br/>";
          ?>
          

          xml:
          http://www.woot.com/salerss.aspx

            D'oh, I'm stupid.

            The 'woot:' means they're using a different namespace... one they've created. As such, you'll need to tell SimpleXML to look in this alternate namespace. Easiest way I can think of would be to use XPath:

            $xml = simplexml_load_file("http://www.woot.com/salerss.aspx"); 
            $xml->registerXPathNamespace('woot', 'http://www.woot.com/');
            $price = $xml->xpath('channel/item[1]/woot:price');
            
            echo $price[0]; // $49.99

              how would i get the product quantity?

                    <woot:products xmlns:woot="http://www.woot.com/">
                      <woot:product quantity="1">Dyson DC07 Cyclonic Bagless Upright Vacuum with Low Reach Attachment</woot:product>
                    </woot:products>
              

              i tried:

              foreach($xml->xpath('channel/item[1]/woot:products/woot:product') as $a => $b) {
                 echo $a,'="',$b,"\"\n";
              }
              

              but i get:

              0="Dyson DC07 Cyclonic Bagless Upright Vacuum with Low Reach Attachment"

              instead of a quantity of 1

                i got it:

                $wootqty = $xml->xpath('channel/item[1]/woot:products/woot:product/@quantity');

                  The code would be:

                  $xml = simplexml_load_file("http://www.woot.com/salerss.aspx"); 
                  $xml->registerXPathNamespace('woot', 'http://www.woot.com/'); 
                  $price = $xml->xpath('channel/item[1]/woot:products[1]/woot:product'); 
                  
                  $quantity = $price[0]->attributes()->quantity;
                  $product = $price[0];

                  For more information on XPath, you might want to check out this tutorial.

                  EDIT: Heh, didn't see your post before I posted.

                    yea i struggled with that attributes() function and simply couldnt get it to work

                    thanks for all the help tho dude

                      Definitely understandable... all of this XPath stuff is new to me as well. In fact, I started learning it in resposne to this thread! :p

                        Write a Reply...