我正在尝试使用2个xml文件将数据放在列表中。我无法编辑一个Xml文件,第二个是我创建的种类的参考数据库。这两个文件都具有<rvXMLfilename>
的公共字符串,如果两个xml文件字符串都匹配,我想从XML文件2中回显一个字符串。我试图将其写入php脚本,但无法使其正常工作。
$railunit = simplexml_load_file('railUnitList.xml');
$orders = simplexml_load_file('train.xml');
foreach ($orders->xpath("//RailVehicleStateClass") as $traininfo):
$rvXMLfilename=$traininfo->rvXMLfilename;
$unitType=$traininfo->unitType;
$unitNumber=$traininfo->unitNumber;
$destinationTag=$traininfo->destinationTag;
$loadWeightUSTons=$traininfo->loadWeightUSTons;
echo "<tr>";
echo "<td>";
if($railunit->rvXMLfilename == $orders->rvXMLfilename){
echo $railunit->unitType;
}else{
echo "error!";
}
XML文件1
<?xml version="1.0"?>
<ScnLoader xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<trainList>
<TrainLoader>
<unitLoaderList>
<RailVehicleStateClass>
<rvXMLfilename>R8_SD40T-2_UP03.xml</rvXMLfilename>
<unitType>US_DieselEngine</unitType>
<loadWeightUSTons>14.2985783</loadWeightUSTons>
<destinationTag>MDTLA</destinationTag>
<unitNumber>8626</unitNumber>
</RailVehicleStateClass>
<RailVehicleStateClass>
<rvXMLfilename>R8_SD40T-2_UP01.xml</rvXMLfilename>
<unitType>US_DieselEngine</unitType>
<loadWeightUSTons>14.2985792</loadWeightUSTons>
<destinationTag>None</destinationTag>
<unitNumber>4401</unitNumber>
</RailVehicleStateClass>
<RailVehicleStateClass>
<rvXMLfilename>R8_SD40T-2_UP01.xml</rvXMLfilename>
<unitType>US_DieselEngine</unitType>
<loadWeightUSTons>14.2985783</loadWeightUSTons>
<destinationTag>None</destinationTag>
<unitNumber>4454</unitNumber>
</RailVehicleStateClass>
</unitLoaderList>
</TrainLoader>
</trainList>
</ScnLoader>
XML文件2
<railUnitList>
<railUnit>
<rvXMLfilename>R8_SD40T-2_UP01.xml</rvXMLfilename>
<unitType>SD40-2T Locomotive</unitType>
<reportingMark>UP</reportingMark>
<axelCount>6</axelCount>
<unitWeight></unitWeight>
<unitLength>71</unitLength>
</railUnit>
<railUnit>
<rvXMLfilename>R8_CoveredHopper_ACF2970_BN01.xml</rvXMLfilename>
<unitType>Covered Hopper</unitType>
<reportingMark>BN</reportingMark>
<axelCount>4</axelCount>
<unitWeight></unitWeight>
<unitLength>60</unitLength>
</railUnit>
<railUnit>
<rvXMLfilename>R8_C14Hopper_POTASH01.xml</rvXMLfilename>
<unitType>Covered Hopper</unitType>
<reportingMark>POTX</reportingMark>
<axelCount>4</axelCount>
<unitWeight></unitWeight>
<unitLength>60</unitLength>
</railUnit>
<railUnit>
<rvXMLfilename>Run8_Tank107BN01.xml</rvXMLfilename>
<unitType>Tank</unitType>
<reportingMark>BN</reportingMark>
<axelCount>4</axelCount>
<unitWeight></unitWeight>
<unitLength>59</unitLength>
</railUnit>
</railUnitList>
答案 0 :(得分:0)
再次使用XPath查询获取您要查找的值。将所有文件名粘贴到数组中,以便以后进行比较。
<?php
$railunit = simplexml_load_file('railUnitList.xml');
$orders = simplexml_load_file('train.xml');
foreach ($railunit->railUnit as $railunit) {
$key = (string)$railunit->unitType;
$value = (string)$railunit->rvXMLfilename;
$filenames[$key] = $value;
}
foreach ($orders->xpath("//RailVehicleStateClass") as $traininfo) {
$rvXMLfilename=$traininfo->rvXMLfilename;
$unitType=$traininfo->unitType;
$unitNumber=$traininfo->unitNumber;
$destinationTag=$traininfo->destinationTag;
$loadWeightUSTons=$traininfo->loadWeightUSTons;
echo "<tr>";
echo "<td>";
if(in_array($traininfo->rvXMLfilename, $filenames)){
echo array_search($traininfo->rvXMLfilename, $filenames);
} else {
echo "error!";
}
}
还要注意您在原始代码中使用$orders->rvXMLfilename
。 PHP结构与XML结构相同,除非它是根元素的直接子元素,否则不能以这种方式引用该元素。
答案 1 :(得分:0)
您的代码有几个问题。首先,您不能直接将SimpleXMLElement
的值引用为例如$rvXMLfilename=$traininfo->rvXMLfilename
;您需要将其转换为适当的类型。其次,rvXMLfilename
不是$railunit
的子级。我认为您可能想遍历$railunit
中的值,并将每个值与从$orders
XML中提取的值进行比较。此代码应实现您想要的:
foreach ($orders->xpath("//RailVehicleStateClass") as $traininfo) {
$rvXMLfilename=(string)$traininfo->rvXMLfilename;
$unitType=(string)$traininfo->unitType;
$unitNumber=(int)$traininfo->unitNumber;
$destinationTag=(string)$traininfo->destinationTag;
$loadWeightUSTons=(float)$traininfo->loadWeightUSTons;
echo "<tr>";
echo "<td>";
$message = "error!";
foreach ($railunit->railUnit as $ru) {
if((string)$ru->rvXMLfilename == $rvXMLfilename){
$message = (string)$ru->unitType;
}
}
echo $message;
}
输出(用于示例数据)
<tr><td>error!<tr><td>SD40-2T Locomotive<tr><td>SD40-2T Locomotive