gpt4 book ai didi

php - 通过 PHP 将不规则 XML 导入 MySQL

转载 作者:行者123 更新时间:2023-11-29 09:57:42 25 4
gpt4 key购买 nike

希望能帮到你。我有一个 xml 文件,我想将其导入到 MySQL 表中。不幸的是,它不是标准的 XML 文件。格式请参见下面:

<?xml version="1.0" encoding="UTF-8"?>
<DDCCommonData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" source="ECloudConnect" AuditDate="2018-11-30T13:11:32.4222315Z">
<CounterNotification>
<Date>2018-11-29T01:48:00</Date>
<MachineModel>Model 123</MachineModel>
<MachineID xsi:nil="true" />
<SerialNumber>CGD67291</SerialNumber>
<SourceDeviceID xsi:nil="true" />
<SourceModelID xsi:nil="true" />
<EncompassID xsi:nil="true" />
<ChargeCounter>
<Counter Mode="PRINT" Color="BLACK" Type="COPY">
<Large>0</Large>
<Small>2043</Small>
</Counter>
<Counter Mode="PRINT" Color="FULL" Type="COPY">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="PRINT" Color="TWIN" Type="COPY">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="PRINT" Color="BLACK" Type="FAX">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="PRINT" Color="FULL" Type="FAX">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="PRINT" Color="TWIN" Type="FAX">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="PRINT" Color="BLACK" Type="PRINT">
<Large>0</Large>
<Small>2470</Small>
</Counter>
<Counter Mode="PRINT" Color="FULL" Type="PRINT">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="PRINT" Color="TWIN" Type="PRINT">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="PRINT" Color="BLACK" Type="LIST">
<Large>0</Large>
<Small>11</Small>
</Counter>
<Counter Mode="PRINT" Color="FULL" Type="LIST">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="PRINT" Color="TWIN" Type="LIST">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="SCAN" Color="BLACK" Type="COPY">
<Large>0</Large>
<Small>1270</Small>
</Counter>
<Counter Mode="SCAN" Color="FULL" Type="COPY">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="SCAN" Color="TWIN" Type="COPY">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="SCAN" Color="BLACK" Type="FAX">
<Large>0</Large>
<Small>21</Small>
</Counter>
<Counter Mode="SCAN" Color="FULL" Type="FAX">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="SCAN" Color="TWIN" Type="FAX">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="SCAN" Color="BLACK" Type="NET">
<Large>1</Large>
<Small>544</Small>
</Counter>
<Counter Mode="SCAN" Color="FULL" Type="NET">
<Large>0</Large>
<Small>0</Small>
</Counter>
<Counter Mode="SCAN" Color="TWIN" Type="NET">
<Large>0</Large>
<Small>0</Small>
</Counter>
</ChargeCounter>
<ErrorHistory xsi:nil="true" />
<DepartmentCodes />

我需要导入以下数据:

机器型号序列号

哪些是简单的,但对于 ChargeCounter 信息来说就变得复杂了。我需要每个计数器模式的所有大数据和小数据:

大号小

这就是我用来导入 MachineModel 和 SerialNumber 的内容,我只需要每个 ChargerCounter 的大计数器和小计数器。

<?php
$conn = mysqli_connect("localhost", "root", "test", "info");

$affectedRow = 0;

$xml = simplexml_load_file("input.xml") or die("Error: Cannot create object");

foreach ($xml->children() as $row) {
$MachineModel = $row->MachineModel ;
$SerialNumber= $row->SerialNumber;

$sql = "INSERT INTO counts(MachineModel ,SerialNumber) VALUES ('" . $MachineModel . "','" . $SerialNumber. "')";

$result = mysqli_query($conn, $sql);

if (! empty($result)) {
$affectedRow ++;
} else {
$error_message = mysqli_error($conn) . "\n";
}
}
?>

****更新****

我需要输出来适应这个表:

CREATE TABLE info (
id int(9) NOT NULL auto_increment,

modelname varchar(255) NULL,
serialnumber varchar(255) NULL,

print_black_copy_sm varchar(255) NULL,
print_black_copy_lg varchar(255) NULL,
print_full_copy_sm varchar(255) NULL,
print_full_copy_lg varchar(255) NULL,
print_twin_copy_sm varchar(255) NULL,
print_twin_copy_lg varchar(255) NULL,

print_black_fax_sm varchar(255) NULL,
print_black_fax_lg varchar(255) NULL,
print_full_fax_sm varchar(255) NULL,
print_full_fax_lg varchar(255) NULL,
print_twin_fax_sm varchar(255) NULL,
print_twin_fax_lg varchar(255) NULL,

print_black_print_sm varchar(255) NULL,
print_black_print_lg varchar(255) NULL,
print_full_print_sm varchar(255) NULL,
print_full_print_lg varchar(255) NULL,
print_twin_print_sm varchar(255) NULL,
print_twin_print_lg varchar(255) NULL,

print_black_list_sm varchar(255) NULL,
print_black_list_lg varchar(255) NULL,
print_full_list_sm varchar(255) NULL,
print_full_list_lg varchar(255) NULL,
print_twin_list_sm varchar(255) NULL,
print_twin_list_lg varchar(255) NULL,

scan_black_copy_sm varchar(255) NULL,
scan_black_copy_lg varchar(255) NULL,
scan_full_copy_sm varchar(255) NULL,
scan_full_copy_lg varchar(255) NULL,
scan_twin_copy_sm varchar(255) NULL,
scan_twin_copy_lg varchar(255) NULL,

scan_black_fax_sm varchar(255) NULL,
scan_black_fax_lg varchar(255) NULL,
scan_full_fax_sm varchar(255) NULL,
scan_full_fax_lg varchar(255) NULL,
scan_twin_fax_sm varchar(255) NULL,
scan_twin_fax_lg varchar(255) NULL,

scan_black_net_sm varchar(255) NULL,
scan_black_net_lg varchar(255) NULL,
scan_full_net_sm varchar(255) NULL,
scan_full_net_lg varchar(255) NULL,
scan_twin_net_sm varchar(255) NULL,
scan_twin_net_lg varchar(255) NULL,

PRIMARY KEY (id)
) ENGINE=InnoDB;

最佳答案

这里有一个技巧。它只是基本上完成 XML 文件。

<?php
$conn = mysqli_connect("localhost", "root", "test", "info");

$affectedRow = 0;

$file_contents = sprintf("%s</CounterNotification> </DDCCommonData>",file_get_contents("input.xml"));

$xml = simplexml_load_string($file_contents) or die("Error: Cannot create object");

foreach ($xml->children() as $row) {
$MachineModel = $row->MachineModel ;
$SerialNumber= $row->SerialNumber;

$sql = "INSERT INTO counts(MachineModel ,SerialNumber) VALUES ('" . $MachineModel . "','" . $SerialNumber. "')";

$result = mysqli_query($conn, $sql);

if (! empty($result)) {
$affectedRow ++;
} else {
$error_message = mysqli_error($conn) . "\n";
}
}

关于php - 通过 PHP 将不规则 XML 导入 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53562047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com