我有一个XML文件,我试图用PHP解析,然后将其插入数据库。
这是XML的一个示例;
<?xml version="1.0" encoding="UTF-8"?>
<feed>
<category name="Parent Cat">
<sub-category name="Child Cat">
<item id="2828" code="AC123">
<productId>2828</productId>
<productCode>AC123</productCode>
<live>1</live>
<productTitle>Product Title</productTitle>
<description><![CDATA[<p>*****</p>]]></description>
<imageLink>http://www.***.co.uk/uploads/images/products/product_2828.jpg</imageLink>
<price>99.99</price>
<rrp>99.99</rrp>
<brandName>Brand</brandName>
<stock>100</stock>
</item>
<item id="2829" code="AC234">
<productId>2829</productId>
<productCode>AC234</productCode>
<live>1</live>
<productTitle>Product Title</productTitle>
<description><![CDATA[<p>*****</p>]]></description>
<imageLink>http://www.***.co.uk/uploads/images/products/product_2829.jpg</imageLink>
<price>99.99</price>
<rrp>99.99</rrp>
<brandName>Brand</brandName>
<stock>100</stock>
</item>
这是我到目前为止已经成功解析项目的PHP代码,直到子类别组结束。
$feed = simplexml_load_file($xml_file);
$counter = 0;
foreach ($feed->category->{'sub-category'}->item as $product) {
$sku = "A1 - " . mysql_real_escape_string($product->productCode);
$products_id = mysql_real_escape_string($product->productId);
$products_code = mysql_real_escape_string($product->productCode);
$live = mysql_real_escape_string($product->live);
$product_title = mysql_real_escape_string($product->productTitle);
$description = mysql_real_escape_string($product->description);
$image_link = mysql_real_escape_string($product->imageLink);
$price = mysql_real_escape_string($product->price);
$rrp = mysql_real_escape_string($product->rrp);
$brand_name = mysql_real_escape_string($product->brandName);
$stock = mysql_real_escape_string($product->stock);
$counter++;
}
我需要代码来处理所有项目,并记录与每个项目关联的类别和子类别。共有8个主要类别,113个子类别和1901个项目。
提前致谢。