如何从XML读取所有产品

时间:2013-02-27 12:39:14

标签: php mysql xml

我的XML文件中有很多类别。我怎样才能阅读其中的所有产品?

它只读取第一类。 我将包含xml文件供您查看。这是我正在使用的PHP代码:

 //get products from xml file
  foreach($xml->CREATED->CATEGORY as $product){

  $atts = $product->PRODUCT->attributes();
  $productitemid = $atts['ITEM'];

  $title   = $product->MODEL;
  $rrp   = $product->RRP;
  $productsdescription   = $product->DESCRIPTION;
  $prodname   = $product->NAME;
   echo  $productitemid.' - ' ;
  // echo $product->id.' - ';
  mysql_query("INSERT INTO products (products_id,products_model,products_price,products_status) VALUES ('$productitemid','$title','$rrp','1')");
  mysql_query("INSERT INTO products_description (products_id,products_name,products_description) VALUES ('$productitemid','$prodname','$productsdescription')");
}

这是XML结构:

 <?xml version="1.0" encoding="iso-8859-1"?>
 <STOREITEMS>
 <CREATED value="Fri Feb 22 1:01:02 GMT 2013">
  <CATEGORY id="441" name=" > test1">
  <PRODUCT ITEM="12796">
   <NAME>test1</NAME>
   <MODEL>bb2018</MODEL>
  <PRICE>2.28</PRICE>
   <RRP>3.99</RRP>
   <THUMB>bb2018s.jpg</THUMB>
    <IMAGE>bb2018.jpg</IMAGE>
     <DESCRIPTION>
       test1
      </DESCRIPTION>
      <POWER/>
      <SIZE/>
      <ATTRIBUTES NAME="Size" ATTRIBUTEID="2">
        <ATTRIBUTEVALUES VALUE="16" TITLE="Small" PRICEADJUST="0.00"/>
         <ATTRIBUTEVALUES VALUE="17" TITLE="Medium" PRICEADJUST="0.00"/>
          <ATTRIBUTEVALUES VALUE="18" TITLE="Large" PRICEADJUST="0.00"/>
          </ATTRIBUTES>
         </PRODUCT>
    <CATEGORY id="442" name=" > test2">
     <PRODUCT ITEM="12805">
    <NAME>test2</NAME>
     <MODEL>bb2034</MODEL>
     <PRICE>0.58</PRICE>
     <RRP>1.50</RRP>
      <THUMB>bb2034s.jpg</THUMB>
      <IMAGE>bb2034.jpg</IMAGE>
      <DESCRIPTION>
        test2
       </DESCRIPTION>
       <POWER/>
      <SIZE/>
        </PRODUCT>
        <CATEGORY id="4423" name=" > test3">
        <PRODUCT ITEM="13719">
         <NAME>test3?</NAME>
         <MODEL>BCPG02</MODEL>
         <PRICE>2.83</PRICE>
         <RRP>4.95</RRP>
       <THUMB>bcg02s.jpg</THUMB>
   <IMAGE>bcpg02.jpg</IMAGE>
     <DESCRIPTION>
     test3
    </DESCRIPTION>
    </PRODUCT>
     </CATEGORY>
     </CREATED>
     </STOREITEMS>

我这样做是有效的。如何从类别中获取产品然后转到下一个cateory并获取dabase需要它们的下一个产品序列

  //i have done it like this it works

 $doc = new DOMDocument();
 $var = $doc->load('shop.xml');

 $root = $doc->documentElement;   //root node
  $items = $doc->getElementsByTagName('PRODUCT');
  $cat = $doc->getElementsByTagName('CATEGORY');

  foreach ($cat as $cats){
  foreach ($items as $bar)

    if ($categoriesid == $b)

$productsid = $bar->getAttribute('ITEM');
$modelcode = $bar->getElementsByTagName('MODEL')->item(0)->nodeValue;
      $rrp = $bar->getElementsByTagName('RRP')->item(0)->nodeValue;
      $productsdescription = $bar->getElementsByTagName('DESCRIPTION')->item(0)->nodeValue;
      $prodname = $bar->getElementsByTagName('NAME')->item(0)->nodeValue;


     $categoriesid = $cats->getAttribute('id');
      $categoriesname = $cats->getAttribute('name');

      }

2 个答案:

答案 0 :(得分:1)

您可以尝试使用DOMDocument:

$doc = new DOMDocument();
$var = $doc->load('yourxml.xml');

$root = $doc->documentElement;   //root node
$items = $doc->getElementsByTagName('product');

foreach ($items as $bar)
{
    $name = $bar->getElementsByTagName('name')->item(0)->nodeValue;
    $model = $bar->getElementsByTagName('model')->item(0)->nodeValue;
    $price = ....
    //do something with the values
}

答案 1 :(得分:0)

请看下面的示例代码 希望这对你有帮助。

<?php
  $xmlString= '<xml Version="1.0">
  <created>
 <category>
   <product id="a"/>
 </category>
 <category>
   <product id="b"/>
 </category>
 <category>
   <product id="c"/>
 </category>
 <category>
   <product id="d"/>
 </category>
 </created>
 </xml>';


$xml = new SimpleXMLElement($xmlString);

foreach ($xml->created->category as $element)
{
  foreach($element as $val)
  {
        echo " product : ".$val->attributes();
  }
}

?>

此代码的输出将读取所有产品属性

product : a product : b product : c product : d