【问题标题】:Finding the first entry of XML and displaying it查找 XML 的第一个条目并显示它
【发布时间】:2020-07-16 07:22:26
【问题描述】:

我正在使用 XML 和 PHP 编译一个列表。我正在寻找在 RailVehicleStateClass 中为每个火车 ID 找到的第一个“目的地标签”并将其回显。我尝试做一个 foreach 循环来收集目标标签,但它只是为每个火车 ID 循环相同的数据,直到 xml 文件结束。下面是 XML 文件的 sn-p,完整版本有 700 多个条目,每列火车可以有 1 到 100 多个与之关联的轨道车辆。

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>
                        <destinationTag>TRC SVMS</destinationTag>
                    </RailVehicleStateClass>
                    <RailVehicleStateClass>
                        <destinationTag>PRC</destinationTag>
                    </RailVehicleStateClass>
            </unitLoaderList>
        </TrainLoader>
    </trainList>
</ScnLoader>

PHP

 <?php
            $trains = simplexml_load_file("Auto%20Save%20World.xml") or die("Error: Cannot create object");   
            $totalUnitCount=0;
            echo "<table>";
                echo "<th>#</th>";
                echo "<th>Train ID</th>";
                echo "<th>Symbol</th>";
                echo "<th>Direction</th>";
                echo "<th>AI</th>";
            foreach ($trains->xpath("//TrainLoader") as $train) {
                $totalUnitCount = $totalUnitCount + 1;

                foreach (array_slice($trains->xpath("//RailVehicleStateClass"),0,1) as $unit){
                    echo $unit->destinationTag;
                }
                echo "</td>";
                echo "<td>";
                echo $train->DispatchTrainDirection;
                echo "</td>";
                echo "<td>";
                echo $train->TrainWasAI;
                echo "</td>";
            }

            ?>

【问题讨论】:

  • 这仍然只是为每一行重复相同的第一个标签
  • 您可以直接使用echo $train-&gt;unitLoaderList-&gt;RailVehicleStateClass-&gt;destinationTag; 而不是运行另一个 XPath 查询。

标签: php xml foreach


【解决方案1】:

您获取RailVehicleStateClass 元素的xpath 查询需要相对于当前$train。你可以这样做:

$train->xpath(".//RailVehicleStateClass")

注意在// 前面使用. 以使路径相对于当前$train 元素。

【讨论】:

    【解决方案2】:

    首先,您需要使用 unitLoaderList 中的 foreach,然后使用您从 foreach 创建的对象中的 xpath,并在查询中的 // 之前添加 .,以仅获取对象内部的内容。

    我真的不明白你想用 HTML 表格完成什么,但这里有一个示例代码,可以处理你的文件和多个没有 HTML 的火车。

            $trains = simplexml_load_file("train.xml") or die("Error: Cannot create object");   
            $totalUnitCount=0;
            foreach ($trains->xpath("//trainList") as $train) {
                $totalUnitCount = $totalUnitCount + 1;
                foreach ($train->xpath(".//unitLoaderList") as $unit){
                    foreach ($unit->xpath(".//RailVehicleStateClass") as $railV){
                        echo $railV->destinationTag[0]; //Assuming all RailVehicle has at list one destination tags if not you has to another foreach with xpath of destinationTag and only get the first one
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-21
      • 2021-10-02
      • 2020-09-07
      • 1970-01-01
      相关资源
      最近更新 更多