gpt4 book ai didi

php - 使用 PHP 将多级 XML 加载到 MYSQL

转载 作者:行者123 更新时间:2023-11-28 23:56:56 26 4
gpt4 key购买 nike

我有以下 XML 文件(作为 .xml)需要导入到 MySQL 中。我面临的问题是它是多层次的,我不知道如何将信息输入数据库。

我可以将单个级别加载到数据库中,但随着每个“患者”的节点发生变化,我假设需要使用 IF 语句。

数据库输出应该是这样的:

link_id          postcode          age          sex          atsi          status          measure_type          measure_done          service_date          measure_value          bp_diastolic          bp_systolic          practice_id     extract_date
------- -------- --- --- ---- ------ ------------ ------------ ------------ ------------- ------------ ----------- ----------- ------------
Patient 1 1234 50 2 4 c BP True 22011999 null 80 128 DEMO 2015-07-13T14:45:55.876Z
Patient 1 1234 50 2 4 c CHOLESTEROL True 05061998 7.2 null null DEMO 2015-07-13T14:45:55.876Z
Patient 1 1234 50 2 4 c HAEMOGLOBIN True 05061998 134 null null DEMO 2015-07-13T14:45:55.876Z
Patient 2 4567 21 1 2 P BP True 18062014 null 72 135 DEMO 2015-07-13T14:45:55.876Z
Patient 2 4567 21 1 2 P HEIGHT True 22011999 161.0 null null DEMO 2015-07-13T14:45:55.876Z
Patient 2 4567 21 1 2 P LDL True 05061998 5.00 null null DEMO 2015-07-13T14:45:55.876Z

每个单独的测量都需要在数据库中有自己的行,并从数据和患者节点中提取信息。

我已经尝试过,但没有成功使用 XPath。

这是 XML:

    <?xml version="1.0" encoding="windows-1252"?>
<MedicalMicrodata xmlns="URL HERE">
<data>
<practice_id>DEMO</practice_id>
<extract_date>2015-07-13T14:45:55.876Z</extract_date>
<population_count>123</population_count>
<extractor_details>Demonstration 123</extractor_details>
<patient>
<link_id>Patient 1</link_id>
<postcode>1234</postcode>
<age>50</age>
<sex>2</sex>
<atsi>4</atsi>
<active>False</active>
<status>C</status>
<measurements>
<bp>
<bp_diastolic>80</bp_diastolic>
<bp_done>True</bp_done>
<bp_service_date>22011999</bp_service_date>
<bp_systolic>128</bp_systolic>
</bp>
<cholesterol>
<cholesterol_done>True</cholesterol_done>
<cholesterol_service_date>05061998</cholesterol_service_date>
<cholesterol_value>7.2</cholesterol_value>
</cholesterol>
<haemoglobin>
<haemoglobin_done>True</haemoglobin_done>
<haemoglobin_service_date>05061998</haemoglobin_service_date>
<haemoglobin_value>134.00</haemoglobin_value>
</haemoglobin>
</measurements>
</patient>
<patient>
<link_id>Patient 2</link_id>
<postcode>4567</postcode>
<age>21</age>
<sex>1</sex>
<atsi>2</atsi>
<active>False</active>
<status>P</status>
<measurements>
<bp>
<bp_diastolic>72</bp_diastolic>
<bp_done>True</bp_done>
<bp_service_date>18062014</bp_service_date>
<bp_systolic>135</bp_systolic>
</bp>
<height>
<height_done>True</height_done>
<height_service_date>22011999</height_service_date>
<height_value>161.0</height_value>
</height>
<ldl>
<ldl_done>True</ldl_done>
<ldl_service_date>05061998</ldl_service_date>
<ldl_value>5.00</ldl_value>
</ldl>
</measurements>
</patient>
</data>
</MedicalMicrodata>

XPATH 代码示例

$header_path=$xml->xpath("data");
$patient_path=$xml->xpath("data/patient");
$bp_path=$xml->xpath("data/patient/measurements/bp");
$cholesterol_path=$xml->xpath("data/patient/measurements/cholesterol");
$haemoglobin_path=$xml->xpath("data/patient/measurements/haemoglobin");
$hba1c_path=$xml->xpath("data/patient/measurements/hba1c");
$hdl_path=$xml->xpath("data/patient/measurements/hdl");
$height_path=$xml->xpath("data/patient/measurements/height");
$ldl_path=$xml->xpath("data/patient/measurements/ldl");
$rbg_path=$xml->xpath("data/patient/measurements/random_blood_glucose");
$triglycerides_path=$xml->xpath("data/patient/measurements/triglycerides");
$weight_path=$xml->xpath("data/patient/measurements/weight");

if (!$header_path) {
foreach ($header_path as $header) {

$extract_date = $header["extract_date"];
$practice_id = $header["practice_id"];

echo $practice_id;

if ($patient_path) {

$postcode = $patient["postcode"];
$age = $patient["age"];
$sex = $patient["sex"];
$atsi = $patient["atsi"];
$status = $patient["status"];


// BP
if ($bp_path) {

foreach ($bp_path as $bp) {

$measure_done = $bp["bp_done"];
$service_date = $bp["bp_service_date"];
$bp_diastolic = $bp["bp_diastolic"];
$bp_systolic = $bp["bp_systolic"];

$sql = "INSERT INTO 'nkpi_pat_measure' (postcode, age, sex, atsi, status, measure_type, measure_done, service_date, bp_diastolic, bp_systolic, practice_id, extract_date)
VALUES ('$postcode', '$age','$sex','$atsi','$status','BP','$measure_done', '$service_date','$bp_diastolic','$bp_systolic','$practice_id','$extract_date')";
$result = $sqlconn->prepare($sql);
$result->execute();

}
}

非常感谢任何帮助。

提前谢谢你。

阿德里安。

最佳答案

根据您的表结构,它主要是这里的苦力:为患者对象创建一个数据传输对象(php 类),用于测量的测量对象和 2 个表:

表病人:

Table patient has address information and other personal information

CREATE TABLE patient (
patient_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
{other patient data}
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4; /* While we are there, lets save some storage space */

为患者添加唯一索引,这样患者记录只能插入一次。

表格尺寸:

Table measurements has the measurements.

这都是标准化的:测量值存储为:

 <measurements>
<bp>
<bp_diastolic>80</bp_diastolic>
<bp_done>True</bp_done>
<bp_service_date>22011999</bp_service_date>
<bp_systolic>128</bp_systolic>
</bp>
<cholesterol>
<cholesterol_done>True</cholesterol_done>
<cholesterol_service_date>05061998</cholesterol_service_date>
<cholesterol_value>7.2</cholesterol_value>
</cholesterol>
<haemoglobin>
<haemoglobin_done>True</haemoglobin_done>
<haemoglobin_service_date>05061998</haemoglobin_service_date>
<haemoglobin_value>134.00</haemoglobin_value>
</haemoglobin>
</measurements>

CREATE TABLE measurements(
measurements_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
patient_id INT NOT NULL,
done BOOLEAN,
service_date DATE,
medical_data_identifier VARCHAR(200),
measured_value TEXT,
FOREIGN KEY (patient_id) REFERENCES patient(patient_id)
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4; /* While we are there, lets save some storage space */

诀窍在于模型 1:n:1 名患者,多次测量,不重复任何数据。

第二个技巧是在 medical_data_identifier 中:

medical_data_identifier is structured as follows: * 1 or multiple records per test.

这将导致收缩压/舒张压的 2 条记录。 >medical_data_identifier=收缩压

value=128

medical_data_identifier=diastolic

value=72

风险:1 天内进行多次测试。为了解决这个问题,您可以做其他事情:将测试存储为 json 或 xml:

medical_data_identifier=bloodpressure

value=80128

这取决于选择使用什么的用途。还有其他解决方案:XML 或 JSON 格式的列(为此使用 mariadb 而不是 mysql:100% 兼容,但只是更多选项)。

或标识测试父数据的父列。

或者在为您提供 1:N:M 数据模型和 3 个表之间的测试标识符表:

  • 病人
  • 测试
  • 测量值

它们之间有关键关系。

然后您只需编写一段 php 代码来插入所有这些。

关于php - 使用 PHP 将多级 XML 加载到 MYSQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31443910/

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