troubleshooting Question

Problem with SimpleXML and namespaces

Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America asked on
PHP
6 Comments1 Solution1798 ViewsLast Modified:
I'm trying to build a RSS feed (actually a GoogleBase XML feed) that contains namespaces using SimpleXML.

Here is the how the namespace is declared:
<rss version ="2.0" xmlns:g="http://base.google.com/ns/1.0">

However, when I add children that contain the namespace, such as g:condition, SimpleXML is stripping the namespace from the tag, i.e <condition> .vs <g:condition>

Can you see if I'm doing something wrong here?
  $xmlstring = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
             . "<rss version =\"2.0\" xmlns:g=\"http://base.google.com/ns/1.0\">"
             . "</rss>\n";
  $xml = simplexml_load_string($xmlstring);
  
  $channelNode = $xml->addChild("channel");
  $channelNode->addChild("title", "Google Base feed for mysite.com");
  $channelNode->addChild("link","http://www.mysite.com/");

  // GO get the taxes once
  $result = (SQL to get my taxes from our databse);
  $taxes = array();
  foreach ($result as $row) {
    $taxes[] = Array($row['country'], $row['state'], $row['rate']);
  } // foreach ($result as $row)

  // $Product is an array of product information
  foreach ($Products AS $Product) {
    if ($Product['active']) {
      $itemNode = $channelNode->addChild("item");
      $itemNode->addChild("title", htmlentities($Product['name']));
      $itemNode->addChild("g:brand", htmlentities($Product['manufacturer_name']));
      $itemNode->addChild("g:condition","NEW");
      $itemNode->addChild("description", "<![CDATA[".$Product['description_short']."]]>");
      $itemNode->addChild("g:expiration_date",$Product['expire_date']);
      $itemNode->addChild("g:id","item_".$Product['id_product']);
      $itemNode->addChild("guid","item_".$Product['id_product']);
      $itemNode->addChild("g:image_link",$Product['image_link']);
      $itemNode->addChild("link",htmlentities($Product['product_link']));
      $itemNode->addChild("g:price",$Product['price'])));
      $itemNode->addChild("g:quantity",$Product["quantity"]);
      $itemNode->addChild("g:product_type",htmlentities("\"\"\"".$Product['primary_product_type']."\"\"\""));
      if ($Product['weight']>0)
        $itemNode->addChild("g:weight",$Product['weight'].' lbs');
      
      $taxNode = $itemNode->addChild("g:tax");
      foreach ($taxes as $row) {
        $taxNode->addChild("g:country",$row[0]);
        $taxNode->addChild("g:region", $row[1]);
        $taxNode->addChild("g:rate", $row[2]);
        $taxNode->addChild("g:tax_ship", $Product['tax_shipping']);
      } // foreach ($taxes as $row)
    } // if ($Product['active'])
  } // foreach ($Products AS $Product)
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 6 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros