PHP创建XML函数

function GetProductItemInfoList($styleId)
 {
  global $ClassPb;
  global $conn_59;
  $arr = explode(',',$styleId);
  $arrCount = count($arr);

  $doc = new DOMDocument('1.0', 'utf-8');
  $doc->formatOutput = true;
  $root = $doc->createElement("Items");
  $doc->appendChild($root);

  for ( $i = 0; $i < $arrCount; $i++)
  {
   $ret = $this->GetProductByStyleId($arr[$i], true);
   $product_count = mysql_num_rows($ret);

   if ($product_count >= 1)
   {
    while ($myrow = mysql_fetch_array($ret))
    {
     $item = $doc->createElement("Item");

     //商品详情
     $itemId = $doc->createElement("ItemId");
     $itemId->setAttribute("desc","商品编号");
     $itemId->appendChild($doc->createTextNode($myrow["StyleId"]));

     $title = $doc->createElement("Title");
     $title ->setAttribute("desc","商品名称");
     $title->appendChild($doc->createTextNode($myrow["Title"]));

     $styleId = $doc->createElement("StyleId");
     $styleId ->setAttribute("desc","款式号");
     $styleId->appendChild($doc->createTextNode($myrow["StyleId"]));

     $brandText = $doc->createElement("BrandText");
     $brandText ->setAttribute("desc","品牌文本");
     $brandText->appendChild($doc->createTextNode($myrow["BrandText"]));

     $categoryText = $doc->createElement("CategoryText");
     $categoryText ->setAttribute("desc","分类文本");
     $categoryText->appendChild($doc->createTextNode($myrow["CategoryText"]));

     $bagSize = $doc->createElement("BagSize");
     $bagSize ->setAttribute("desc","包体尺寸");
     $bagSize->appendChild($doc->createTextNode($myrow["Size"]));

     $weight = $doc->createElement("Weight");
     $weight ->setAttribute("desc","重量");
     $weight->appendChild($doc->createTextNode($myrow["Weight"]));

     $upShelfTime = $doc->createElement("UpShelfTime");
     $upShelfTime ->setAttribute("desc","上架时间");
     $upShelfTime->appendChild($doc->createTextNode($myrow["UpShelfTime"]));

     //产品
     $products = $doc->createElement("Products");
     $ret_prod = $this->GetProductByStyleId($arr[$i], false);

     while ($myprod = mysql_fetch_array($ret_prod))
     {
      $product = $products->appendChild($doc->createElement("Product"));

      $productId = $product->appendChild($doc->createElement("ProductId"));
      $productId ->setAttribute("desc","商品编号");
      $productId->appendChild($doc->createTextNode($myprod["ProductId"]));

      $marketPrice = $product->appendChild($doc->createElement("MarketPrice"));
      $marketPrice ->setAttribute("desc","市场价");
      $marketPrice->appendChild($doc->createTextNode($myprod["MarketPrice"]));

      $salePrice = $product->appendChild($doc->createElement("SalePrice"));
      $salePrice ->setAttribute("desc","销售价");
      $salePrice->appendChild($doc->createTextNode($myprod["SalePrice"]));

      $colorText = $product->appendChild($doc->createElement("ColorText"));
      $colorText ->setAttribute("desc","颜色文本");
      $colorText->appendChild($doc->createTextNode($myprod["ColorText"]));

      //大图
      $largeImages = $product->appendChild($doc->createElement("LargeImages"));
      
      //小图
      $smallImages = $product->appendChild($doc->createElement("SmallImages"));
      
      $large_ret = $this->GetProductInfoItemImg($myprod["ProductId"], 12);
      while ($mylarge = mysql_fetch_array($large_ret))
      {
       $largeImage = $largeImages->appendChild($doc->createElement("LargeImage"));
       $largeImage->appendChild($doc->createTextNode("
http://image.yzy.com/" . $myprod["ProductId"] . "/" . $mylarge["LevelId"] . "/1024/1024/p.jpg"));
       
       $smallImage = $smallImages->appendChild($doc->createElement("SmallImage"));
       $smallImage->appendChild($doc->createTextNode("
http://image.yzy.com/" . $myprod["ProductId"] . "/" . $mylarge["LevelId"] . "/62/62/p.jpg"));
      }

      //颜色图
      $colorImage = $product->appendChild($doc->createElement("ColorImage"));
      $colorImage->appendChild($doc->createTextNode("颜色图"));

      //主图
      $listImage = $product->appendChild($doc->createElement("ListImage"));
      $listImage->appendChild($doc->createTextNode("主图"));

      //模特图
      $modelImages = $product->appendChild($doc->createElement("ModelImages"));
      $modelImage = $modelImages->appendChild($doc->createElement("ModelImage"));
      $modelImage->appendChild($doc->createTextNode("模特图"));
     }

     //属性
     $attributes = $doc->createElement("Attributes");
     $attr_ret = $this->GetProductAttributesList($myrow["ProductId"]);
     while ($prod_attribute = mysql_fetch_row($attr_ret))
     {
      $attribute = $attributes->appendChild($doc->createElement("Attribute"));
      $attribute->setAttribute("name","$prod_attribute[0]");
      $attribute->setAttribute("value","$prod_attribute[1]");
     }

     //模特图
     $modelImages = $doc->createElement("ModelImages");
     $modelImage = $modelImages->appendChild($doc->createElement("ModelImage"));
     $modelImage->appendChild($doc->createTextNode("模特图root"));

     // 产品模特图
     $productImages = $doc->createElement("ProductImages");
     $productImage = $productImages->appendChild($doc->createElement("ProductImage"));
     $productImage->appendChild($doc->createTextNode("产品模特图root"));

     $content = $doc->createElement("Content");
     $content ->setAttribute("desc","商品描述");
     $cdata = $content->appendChild($doc->createCDATASection($myrow["Content"]));
     $content->appendChild($cdata);

     //商品详情
     $item->appendChild($itemId);
     $item->appendChild($title);
     $item->appendChild($styleId);
     $item->appendChild($brandText);
     $item->appendChild($categoryText);
     $item->appendChild($bagSize);
     $item->appendChild($weight);
     $item->appendChild($upShelfTime);

 

     //产品
     $item->appendChild($products);

 

     //属性和内容
     $item->appendChild($attributes);

 

     $item->appendChild($modelImages);
     $item->appendChild($productImages);


     $item->appendChild($content);

     $root->appendChild($item);
    }

    $ClassPb->FreeResource($ret);
   }
  }

  $xmlString = $doc->saveXML();

  //输出XML字符串
  header('Content-Type: text/xml; charset=utf-8');

  return $xmlString;
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值