troubleshooting Question

Problem with SimpleXML and namespaces

Avatar of Eddie Shipman
Eddie ShipmanFlag for United States of America asked on
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="">

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=\"\">"
             . "</rss>\n";
  $xml = simplexml_load_string($xmlstring);
  $channelNode = $xml->addChild("channel");
  $channelNode->addChild("title", "Google Base feed for");

  // 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("description", "<![CDATA[".$Product['description_short']."]]>");
      if ($Product['weight']>0)
        $itemNode->addChild("g:weight",$Product['weight'].' lbs');
      $taxNode = $itemNode->addChild("g:tax");
      foreach ($taxes as $row) {
        $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