将XML文件导入MySQL使用一个简单的PHP脚本。

huangapple go评论75阅读模式
英文:

Import XML file into MySQL using a simple PHP script

问题

以下是您要翻译的内容:

"我已经尝试了许多多个站点/论坛上的帮助页面,但仍然遇到困难。

我只想从在线URL/文件中简单地导入XML数据到MySQL数据库。

https://www.geeksforgeeks.org/how-to-load-xml-data-into-mysql-using-php/ 对样本数据有效,尽管我要处理的XML数据源结构不同。

这是我要处理的XML数据源...

<STOREITEMS>
		<PRODUCT ITEM='sku-01' NAME='This Is Title 1'>
			<STOCK>
				In Stock
			</STOCK>
		</PRODUCT>
		
		
		
		<PRODUCT ITEM='sku-02' NAME='This Is Title 2'>
			<STOCK>
				In Stock
			</STOCK>
		</PRODUCT>
		
		
		
		<PRODUCT ITEM='sku-03' NAME='This Is Title 3'>
			<STOCK>
				No Stock.
			</STOCK>
		</PRODUCT>
	</STOREITEMS>

任何帮助将不胜感激。

我使用了geekforgeeks的以下代码...

<?php
 
// 连接到数据库
// 服务器 - localhost
// 用户名 - root
// 密码 - 空
// 数据库名称 = xmldata
$conn = mysqli_connect("localhost", "root", "empty", "xmldata");
 
$affectedRow = 0;
 
// 加载XML文件,否则检查连接
$xml = simplexml_load_file("xmltest.xml")
    or die("错误:无法创建对象");
 
// 分配值
foreach ($xml->children() as $row) {

    $product = $row->product;
    $stock = $row->stock;
     

    $sql = "INSERT INTO xtrader(
        SKU, stock) VALUES ('"
        . $product . "',' " . $stock . "')";     
    $result = mysqli_query($conn, $sql);
     
    if (! empty($result)) {
        $affectedRow ++;
    } else {
        $error_message = mysqli_error($conn) . "\n";
    }
}
?>
 
<center><h2>GEEKS GOR GEEKS</h2></center>
<center><h1>XML数据存储在数据库中</h1></center>
<?php
if ($affectedRow > 0) {
    $message = $affectedRow . "条记录已插入";
} else {
    $message = "未插入记录";
}
 
?>
<style>
    body { 
        max-width:550px;
        font-family: Arial;
    }
    .affected-row {
        background: #cae4ca;
        padding: 10px;
        margin-bottom: 20px;
        border: #bdd6bd 1px solid;
        border-radius: 2px;
        color: #6e716e;
    }
    .error-message {
        background: #eac0c0;
        padding: 10px;
        margin-bottom: 20px;
        border: #dab2b2 1px solid;
        border-radius: 2px;
        color: #5d5b5b;
    }
</style>
 
<div class="affected-row">
    <?php  echo $message; ?>
</div>
 
<?php if (! empty($error_message)) { ?>
 
<div class="error-message">
    <?php echo nl2br($error_message); ?>
</div>
<?php } ?>

没有导入任何内容。"

英文:

I have tried so many help pages on multpl sites/forums and am still struggling here.

I am looking to simply import XML data from an online URL/file into a MySQL database.

The https://www.geeksforgeeks.org/how-to-load-xml-data-into-mysql-using-php/ worked with the sample data though the XML data feed I have to work with is structured differently.

This is the XML data feed I have...
<?xml version="1.0" encoding="UTF-8"?>

&lt;STOREITEMS&gt;
		&lt;PRODUCT ITEM=&#39;sku-01&#39; NAME=&#39;This Is Title 1&#39;&gt;
			&lt;STOCK&gt;
				In Stock
			&lt;/STOCK&gt;
		&lt;/PRODUCT&gt;
		
		
		
		&lt;PRODUCT ITEM=&#39;sku-02&#39; NAME=&#39;This Is Title 2&#39;&gt;
			&lt;STOCK&gt;
				In Stock
			&lt;/STOCK&gt;
		&lt;/PRODUCT&gt;
		
		
		
		&lt;PRODUCT ITEM=&#39;sku-03&#39; NAME=&#39;This Is Title 3&#39;&gt;
			&lt;STOCK&gt;
				No Stock.
			&lt;/STOCK&gt;
		&lt;/PRODUCT&gt;
	&lt;/STOREITEMS&gt;

Any and all help would be appreciated.

I have used the geekforgeeks code as below...

&lt;?php
 
// Connect to database
// Server - localhost
// Username - root
// Password - empty
// Database name = xmldata
$conn = mysqli_connect(&quot;localhost&quot;, &quot;root&quot;, &quot;empty&quot;, &quot;xmldata&quot;);
 
$affectedRow = 0;
 
// Load xml file else check connection
$xml = simplexml_load_file(&quot;xmltest.xml&quot;)
    or die(&quot;Error: Cannot create object&quot;);
 
// Assign values
foreach ($xml-&gt;children() as $row) {

    $product = $row-&gt;product;
    $stock = $row-&gt;stock;
     

	$sql = &quot;INSERT INTO xtrader(
        SKU, stock) VALUES (&#39;&quot;
        . $product . &quot;&#39;,&#39;&quot; . $stock . &quot;&#39;)&quot;;     
    $result = mysqli_query($conn, $sql);
     
    if (! empty($result)) {
        $affectedRow ++;
    } else {
        $error_message = mysqli_error($conn) . &quot;\n&quot;;
    }
}
?&gt;
 
&lt;center&gt;&lt;h2&gt;GEEKS GOR GEEKS&lt;/h2&gt;&lt;/center&gt;
&lt;center&gt;&lt;h1&gt;XML Data storing in Database&lt;/h1&gt;&lt;/center&gt;
&lt;?php
if ($affectedRow &gt; 0) {
    $message = $affectedRow . &quot; records inserted&quot;;
} else {
    $message = &quot;No records inserted&quot;;
}
 
?&gt;
&lt;style&gt;
    body { 
        max-width:550px;
        font-family: Arial;
    }
    .affected-row {
        background: #cae4ca;
        padding: 10px;
        margin-bottom: 20px;
        border: #bdd6bd 1px solid;
        border-radius: 2px;
        color: #6e716e;
    }
    .error-message {
        background: #eac0c0;
        padding: 10px;
        margin-bottom: 20px;
        border: #dab2b2 1px solid;
        border-radius: 2px;
        color: #5d5b5b;
    }
&lt;/style&gt;
 
&lt;div class=&quot;affected-row&quot;&gt;
    &lt;?php  echo $message; ?&gt;
&lt;/div&gt;
 
&lt;?php if (! empty($error_message)) { ?&gt;
 
&lt;div class=&quot;error-message&quot;&gt;
    &lt;?php echo nl2br($error_message); ?&gt;
&lt;/div&gt;
&lt;?php } ?&gt;

Nothing is importing.

答案1

得分: 1

$product = $row->product;

因为&lt;PRODUCT&gt;元素本身就是一行,所以没有$row->product。您想要的是该元素中名为ITEM的属性:

$attributes = $row->attributes();
$sku = (string) $attributes['ITEM'];

或者:

$sku = (string) $row->attributes()['ITEM'];

或者只需:

$sku = (string) $row['ITEM'];

还要注意,$row->STOCK将获取该属性中的所有内容,包括空格:

$stock = (string) $row->STOCK;
var_dump($stock);

会得到:

string(38) "
                In Stock
            "

这可能不是您想要的,所以使用trim()去除多余的空格:

$stock = trim((string) $row->STOCK);

其他注意事项:

  • 您的某些项目使用No Stock,而某些项目使用No Stock.带有句点。如果您基于此设置条件,可能会遇到问题。此值可能应该是一个简单的整数表示,因此“无库存”条件将简单为<STOCK>0</STOCK>
  • 如果您将SKU和NAME放在属性中,最好将库存也放在属性中,如<PRODUCT ITEM='sku-01' NAME='This Is Title 1' STOCK='0'>。将库存作为单独的元素放置的唯一原因是,如果每个项目可以有多个值,这在这种情况下似乎没有意义。
  • 不要忽略SQL注入问题,这是一个重要问题。
英文:
$product = $row-&gt;product;

There is no $row-&gt;product because the &lt;PRODUCT&gt; element is itself the row. You want the attribute named ITEM in that element:

$attributes = $row-&gt;attributes();
$sku = (string) $attributes[&#39;ITEM&#39;];

Or:

$sku = (string) $row-&gt;attributes()[&#39;ITEM&#39;];

Or just:

$sku = (string) $row[&#39;ITEM&#39;];

Also note that $row-&gt;STOCK will get you everything in that attribute, including whitespace:

$stock = (string) $row-&gt;STOCK;
var_dump($stock);

Yields:

string(38) &quot;
            In Stock
        &quot;

This is probably not what you want, so trim() the excess:

$stock = trim((string) $row-&gt;STOCK);

Other concerns:

  • Some of your items use No Stock and some use No Stock. with a period. If you base conditionals on this, you're gonna have a bad time. This value probably should be a simple integer representation, so the "no stock" condition would be simply &lt;STOCK&gt;0&lt;/STOCK&gt;.
  • If you're going to put the SKU and NAME in attributes, you might as well put the stock in an attribute too, like &lt;PRODUCT ITEM=&#39;sku-01&#39; NAME=&#39;This Is Title 1&#39; STOCK=&#39;0&#39;&gt;. The only reason you'd put the stock as a separate element is if you can have more than one value per item, which doesn't seem to make sense here.
  • Don't ignore your SQL injections, that's a big deal.

huangapple
  • 本文由 发表于 2023年3月21日 00:35:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75792964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定