我有这个代码从我服务器上的xml文件插入mysql db
$xml = simplexml_load_file('/var/www/file.xml');
foreach( $xml->product as $value )
foreach( $xml->product->fields as $categories )
{
$name = mysql_real_escape_string($value->name);
$number = mysql_real_escape_string($value->sku);
$description = mysql_real_escape_string($value->description);
$CategoryPath = mysql_real_escape_string($categories->breadCrumb);
$sql = "
INSERT INTO table SET
name = '$name',
sku = '$number',
description = '$description',
category = '$CategoryPath'
";
$rssql = mysql_query( $sql );
}
这是xml结构
<product>
<name>Ghost Design 2000 Supporto TV LCD rotation</name>
<description>
description text...
</description>
<brand>MELICONI</brand>
<sku>8006023191062</sku>
<fields>
<breadCrumb>
Home|Tecnologia e intrattenimento|Tv, DVD, Home Cinema|Accessori Tv/Video|Supporti video
</breadCrumb>
</fields>
</product>
我在我的sql表中插入所有记录但是未正确插入字段breadCrumb。它总是为所有记录插入相同的breadCrumb数据。
答案 0 :(得分:0)
$xml = '<?xml version="1.0"?>
<products>
<product>
<name>Ghost Design 2000 Supporto TV LCD rotation</name>
<description>description text...</description>
<brand>MELICONI</brand>
<sku>8006023191062</sku>
<fields>
<breadCrumb>
Home|Tecnologia e intrattenimento|Tv, DVD, Home Cinema|Accessori Tv/Video|Supporti video
</breadCrumb>
</fields>
</product>
<product>
<name>Ghost Design 2000 Supporto TV LCD rotation</name>
<description>
description text...
</description>
<brand>MELICONI</brand>
<sku>8006023191062</sku>
<fields>
<breadCrumb>
second
</breadCrumb>
</fields>
</product>
</products>';
$parsedXml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA);
foreach($parsedXml->product as $product){
foreach($product->fields as $categories)
echo $categories->breadCrumb.'<br>';
}
那应该对你有所帮助!
一些额外信息:
在PHP 5.5 mysql_query函数已弃用,请改用<{3}}
答案 1 :(得分:0)
尝试使用
<![CDATA[
喜欢:
<breadCrumb><![CDATA[]]>
Home|Tecnologia e intrattenimento|Tv, DVD, Home Cinema|Accessori Tv/Video|Supporti video
]]></breadCrumb>
还要确保使用mysql_real_escape_string将其放入数据库