0

我的 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>

我已经这样做了,它可以工作。我如何从类别中获取产品然后转到下一个类别并获取数据库需要的下一个产品序列

  //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');

      }
4

2 回答 2

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
}
于 2013-02-27T13:21:38.213 回答
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
于 2013-02-27T13:07:05.153 回答