我正在尝试使用此XML输出中的某些数据创建一个数组。考虑到数据的结构方式,我发现它非常棘手。我正在尝试仅使用MECHANICAL
description
数据形成一个数组。我希望我的输出看起来像:
Mechanical
Engine: 2.0L DOHC 4-Cylinder TFSI
Transmission: 8-Speed Automatic w/Tirptronic
Full-Time All-Wheel Drive
等等...
到目前为止,我一直在尝试这样称呼机械描述:
<?php
$xml = file_get_contents('note.xml');
$dom = new DOMDocument();
$dom->loadXML($xml);
foreach ($dom->getElementsByTagName('standard') as $standard){
$mechdescription = $standard->getElementsByTagName('description');
$mecharray[] = $mechdescription;
}
foreach ($mecharray as $displaymech){
echo $displaymech;
}
?>
这是我要从中提取的XML的一部分:
<VehicleDescription xmlns="urn:description7b.services.chrome.com" country="US" language="en" modelYear="2015" bestMakeName="Audi" bestModelName="A4" bestStyleName="4dr Sdn Auto quattro 2.0T Premium" bestTrimName="Premium">
<standard>
<header id="1236">MECHANICAL</header>
<description>Engine: 2.0L DOHC 4-Cylinder TFSI -inc: Audi valvelift system</description>
<category id="1048"/>
<category id="1054"/>
<category id="1213"/>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>Transmission: 8-Speed Automatic w/Tiptronic -inc: sport program and manual shift mode</description>
<category id="1130"/>
<category id="1195"/>
<category id="1220"/>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>Full-Time All-Wheel Drive</description>
<category id="1041"/>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>Engine Oil Cooler</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>80-Amp/Hr Maintenance-Free Battery w/Run Down Protection</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>150 Amp Alternator</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>Gas-Pressurized Shock Absorbers</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>Front And Rear Anti-Roll Bars</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>Electric Power-Assist Speed-Sensing Steering</description>
<category id="1084"/>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>16.1 Gal. Fuel Tank</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>Single Stainless Steel Exhaust</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>Multi-Link Front Suspension w/Coil Springs</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>Multi-Link Rear Suspension w/Coil Springs</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1236">MECHANICAL</header>
<description>4-Wheel Disc Brakes w/4-Wheel ABS, Front Vented Discs, Brake Assist, Hill Hold Control and Electric Parking Brake</description>
<category id="1018"/>
<category id="1020"/>
<category id="1228"/>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Wheels: 8.0J x 17" 10-Spoke-Star-Design</description>
<category id="1123"/>
<styleId>369101</styleId>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Tires: P245/45R17 AS</description>
<category id="1092"/>
<category id="1097"/>
<styleId>369101</styleId>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Wheels w/Silver Accents</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Steel Spare Wheel</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Compact Spare Tire Mounted Inside Under Cargo</description>
<category id="1098"/>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Clearcoat Paint</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Express Open/Close Sliding And Tilting Glass 1st Row Sunroof w/Sunshade</description>
<category id="1069"/>
<category id="1132"/>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Body-Colored Front Bumper</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Body-Colored Rear Bumper</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Rocker Panel Extensions</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
<standard>
<header id="1176">EXTERIOR</header>
<description>Aluminum Side Windows Trim and Black Front Windshield Trim</description>
<styleId>369101</styleId>
<installed cause="BaseEquipment"/>
</standard>
</VehicleDescription>
任何帮助将不胜感激。
更新1
我已经确定了如何接收description
标签的所有standard
。但是,我只想要description
的{{1}}数据。这是我当前的代码,遵循我过去的逻辑。我可以为此写一个MECHANICAL
语句吗?
IF
更新2
@ splash58建议使用xPath是解决此问题的最佳方法。在他的指导下,这是我当前的代码。我仍然没有收到任何回应。
foreach ($dom->getElementsByTagName('standard') as $standard){
$mechdescription = $standard->getElementsByTagName('description')->item(0)->nodeValue;
$mecharray[] = $mechdescription;
}
foreach ($mecharray as $displaymech){
echo $displaymech."<br>";
}
答案 0 :(得分:1)
您可以使用xpath选择符合条件的节点
$dom = simplexml_load_string($xml);
foreach ($dom->xpath('//standard[header="MECHANICAL"]/description') as $mechdescription){
$mecharray[] = $mechdescription;
}
更新
如果要使用domDocument
$dom = new DOMDocument();
$dom->loadXML($xml);
$xpath = new domXpath($dom);
foreach ($xpath->query('//standard[header="MECHANICAL"]/description') as $mechdescription){
$mecharray[] = $mechdescription->nodeValue ;
}
答案 1 :(得分:0)
您应该尝试首先将给定的 XML字符串转换为 XML 并转换为 JSON ,然后将其转换为数组。
$pureXml = simplexml_load_string($strXml, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($pureXml);
$arr = json_decode($json,TRUE);
foreach($arr as $subArr){
if($subArr['header'] == 'MECHANICAL'){
print $subArr['header] . "<br>" . $subArr['description'];
}
}
答案 2 :(得分:0)
我发现以下内容列出了标准标记中的所有说明。仍在尝试弄清楚如何仅使用MECHANICAL
标头调用那些标头。
foreach ($dom->getElementsByTagName('standard') as $standard){
$mechdescription = $standard->getElementsByTagName('description')->item(0)->nodeValue;
$mecharray[] = $mechdescription;
}
foreach ($mecharray as $displaymech){
echo $displaymech."<br>";
}