php - 一种基于唯一 ID 限制数据的方法
问题描述
我有一个包含火车列表的 xml 文件,每列火车都与特定的 trainID 相关联。我编写了一个 php 脚本来将此数据编译为用户可读的格式,但无法找到一种方法来设置它以仅读取和报告与 trainID 关联的数据。我希望通过使用火车 ID 的 url 链接到火车报告页面。我尝试制作一个功能,但我认为这不是我需要的,因为它不起作用。下面是我的 XML 文件和列表生成器的一部分。
<ScnLoader xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<trainList>
<TrainLoader>
<trainID>99991</trainID>
<TrainWasAI>false</TrainWasAI>
<DispatchTrainDirection>0</DispatchTrainDirection>
<ManuallyAppliedSpeedLimitMPH>2147483647</ManuallyAppliedSpeedLimitMPH>
<PreviousSignalInstruction>Clear</PreviousSignalInstruction>
<unitLoaderList>
<RailVehicleStateClass>
<rvXMLfilename>R8_CoveredHopper_PS4750_DGHX01.xml</rvXMLfilename>
<unitType>US_Freightcar</unitType>
<currentRoutePrefix>
<int>320</int>
<int>320</int>
</currentRoutePrefix>
<currentTrackSectionIndex>
<int>1365</int>
<int>1365</int>
</currentTrackSectionIndex>
<startNodeIndex>
<int>0</int>
<int>0</int>
</startNodeIndex>
<distanceTravelledInMeters>
<float>76.0736</float>
<float>90.00746</float>
</distanceTravelledInMeters>
<reverseDirection>
<boolean>true</boolean>
<boolean>true</boolean>
</reverseDirection>
<loadWeightUSTons>111.1</loadWeightUSTons>
<destinationTag>BAR HOU</destinationTag>
<unitNumber>571555</unitNumber>
</RailVehicleStateClass>
</unitLoaderList>
</TrainLoader>
</trainList>
</ScnLoader>
php
<?php
$railunit = simplexml_load_file('railUnitList.xml'); //database of railunits
$orders = simplexml_load_file('testdata.xml'); //Where the data comes from to form the list
?><pre><?php print_r($orders); ?></pre><?php
foreach ($orders->TrainLoader->trainID as $trainID){
$trainid = "99991";
}
foreach ($orders->xpath("RailVehicleStateClass") as $traininfo) {
$rvXMLfilename=(string)$traininfo->rvXMLfilename;
$unitType=(string)$traininfo->unitType;
$unitNumber=(int)$traininfo->unitNumber;
$destinationTag=(string)$traininfo->destinationTag;
$loadWeightUSTons=(int)$traininfo->loadWeightUSTons;
$totalUnitCount = $totalUnitCount + 1;
echo "<tr>";
echo "<td align='center'>";
echo $totalUnitCount;
echo "</td>";
echo "<td>";
foreach ($railunit->railUnit as $ru) {
if((string)$ru->rvXMLfilename == $rvXMLfilename){
$message = (string)$ru->reportingMark;
}
}
echo $message;
echo "</td>";
echo "<td>";
echo $unitNumber;
echo "</td>";
echo "<td>";
$message = "Not Found!";
foreach ($railunit->railUnit as $ru) {
if((string)$ru->rvXMLfilename == $rvXMLfilename){
$message = (string)$ru->unitType;
}
}
}
?>
解决方案
您可以在 XPath 表达式中添加一个条件来仅检索您想要的节点:
$railunit = $railunit->xpath("//TrainLoader[trainID = $trainid]")[0]; // retrieve all TrainLoader node that has a child 'trainID' with value '$trainid', then keep only the first one
// whole node
echo $railunit->asXML() ;
// access specific information
echo (string) $railunit->xpath('./ManuallyAppliedSpeedLimitMPH')[0]; // 2147483647
推荐阅读
- for-loop - 在 Verilog 中,当在顺序过程中使用 for 循环时,如何增加顺序变量?
- angular - Angular - 从 Firebase 填充表单导致重复
- c# - 尝试获取用户名 .net core 2.1 时,用户始终为空
- javacv - 如何将视频转换为 mp4 [javacv]
- tensorflow-agents - py_environment 'time_step' 与 'time_step_spec' 不匹配
- javascript - 如何以用户在 jquery 中输入的次数显示相同的 div?
- node.js - 我想要一个每天自动递减值的函数
- haskell - 如何在编译器中实现源映射?
- linux - 在第三个目录的两个文件之间创建符号链接
- pycharm - 你能在一个活跃的 anaconda 环境中运行 pycharm 的调试器吗?