- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 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/
我在 JavaScript 文件中运行 PHP,例如...... var = '';). 我需要使用 JavaScript 来扫描字符串中的 PHP 定界符(打开和关闭 PHP 的 )。 我已经知道使
我希望能够做这样的事情: php --determine-oldest-supported-php-version test.php 并得到这个输出: 7.2 也就是说,php 二进制检查 test.
我正在开发一个目前不使用任何框架的大型 php 站点。我的大问题是,随着时间的推移慢慢尝试将框架融入应用程序是否可取,例如在创建的新部件和更新的旧部件中? 比如所有的页面都是直接通过url服务的,有几
下面是我的源代码,我想在同一页面顶部的另一个 php 脚本中使用位于底部 php 脚本的变量 $r1。我需要一个简单的解决方案来解决这个问题。我想在代码中存在的更新查询中使用该变量。 $name)
我正在制作一个网站,根据不同的情况进行大量 PHP 重定向。就像这样...... header("Location: somesite.com/redirectedpage.php"); 为了安全起见
我有一个旧网站,我的 php 标签从 因为短标签已经显示出安全问题,并且在未来的版本中将不被支持。 关于php - 如何避免在 php 文件中写入
我有一个用 PHP 编写的配置文件,如下所示, 所以我想用PHP开发一个接口(interface),它可以编辑文件值,如$WEBPATH , $ACCOUNTPATH和 const值(value)观
我试图制作一个登录页面来学习基本的PHP,首先我希望我的独立PHP文件存储HTML文件的输入(带有表单),但是当我按下按钮时(触发POST到PHP脚本) )我一直收到令人不愉快的错误。 我已经搜索了S
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: What is the max key size for an array in PHP? 正如标题所说,我想知道
我正在寻找一种让 PHP 以一种形式打印任意数组的方法,我可以将该数组作为赋值包含在我的(测试)代码中。 print_r 产生例如: Array ( [0] => qsr-part:1285 [1]
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 9 年前。 Improve this ques
我在 MySQL 数据库中有一个表,其中存储餐厅在每个工作日和时段提供的菜单。 表结构如下: i_type i_name i_cost i_day i_start i_
我有两页。 test1.php 和 test2.php。 我想做的就是在 test1.php 上点击提交,并将 test2.php 显示在 div 中。这实际上工作正常,但我需要向 test2.php
我得到了这个代码。我想通过textarea更新mysql。我在textarea中回显我的MySQL,但我不知道如何更新它,我应该把所有东西都放进去吗,因为_GET模式没有给我任何东西,我也尝试_GET
首先,我是 php 的新手,所以我仍在努力学习。我在 Wordpress 上创建了一个表单,我想将值插入一个表(data_test 表,我已经管理了),然后从 data_test 表中获取所有列(id
我有以下函数可以清理用户或网址的输入: function SanitizeString($var) { $var=stripslashes($var); $va
我有一个 html 页面,它使用 php 文件查询数据库,然后让用户登录,否则拒绝访问。我遇到的问题是它只是重定向到 php 文件的 url,并且从不对发生的事情提供反馈。这是我第一次使用 html、
我有一个页面充满了指向 pdf 的链接,我想跟踪哪些链接被单击。我以为我可以做如下的事情,但遇到了问题: query($sql); if($result){
我正在使用 从外部文本文件加载 HTML/PHP 代码 $f = fopen($filename, "r"); while ($line = fgets($f, 4096)) { print $l
我是一名优秀的程序员,十分优秀!