英文:
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"?>
<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>
Any and all help would be appreciated.
I have used the geekforgeeks code as below...
<?php
// Connect to database
// Server - localhost
// Username - root
// Password - empty
// Database name = xmldata
$conn = mysqli_connect("localhost", "root", "empty", "xmldata");
$affectedRow = 0;
// Load xml file else check connection
$xml = simplexml_load_file("xmltest.xml")
or die("Error: Cannot create object");
// Assign values
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 Data storing in Database</h1></center>
<?php
if ($affectedRow > 0) {
$message = $affectedRow . " records inserted";
} else {
$message = "No records inserted";
}
?>
<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 } ?>
Nothing is importing.
答案1
得分: 1
$product = $row->product;
因为<PRODUCT>
元素本身就是一行,所以没有$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->product;
There is no $row->product
because the <PRODUCT>
element is itself the row. You want the attribute named ITEM
in that element:
$attributes = $row->attributes();
$sku = (string) $attributes['ITEM'];
Or:
$sku = (string) $row->attributes()['ITEM'];
Or just:
$sku = (string) $row['ITEM'];
Also note that $row->STOCK
will get you everything in that attribute, including whitespace:
$stock = (string) $row->STOCK;
var_dump($stock);
Yields:
string(38) "
In Stock
"
This is probably not what you want, so trim()
the excess:
$stock = trim((string) $row->STOCK);
Other concerns:
- Some of your items use
No Stock
and some useNo 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<STOCK>0</STOCK>
. - If you're going to put the SKU and NAME in attributes, you might as well put the stock in an attribute too, like
<PRODUCT ITEM='sku-01' NAME='This Is Title 1' STOCK='0'>
. 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论