基于ID的DOMDocument提取XML数据

时间:2018-10-02 20:01:24

标签: php arrays xml foreach domdocument

我正在使用DOMDocumentforeach循环从XML文档中提取数据。数据来自VIN解码器。输入车辆的VIN,然后您将获得有关其的数据。

我苦苦挣扎的方面是,VIN解码器返回了按该车辆样式细分的车辆数据。例如,奥迪S4的VIN返回两种样式:5dr Avant Wgn Man5dr Avant Wgn Auto。每个样式都有一个ID,应用于该样式的数据也带有该ID。

这是这两种样式的ID结构:
5dr Avant Wgn Man - ID: 292015
5dr Avant Wgn Auto - ID: 292016

以及如何将数据绑定到这些ID的示例:

    <technicalSpecification>
        <titleId>2</titleId>
        <value value="5 Door Wagon" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>

请注意,5门旅行车适用于两种样式。

我想要实现的是,当我选择5dr Avant Wgn Man时,将显示与该样式相关的所有<technicalSpecification>数据。同样,如果选择5dr Avant Wgn Auto,则将显示与该样式相关的所有数据。

这是XML:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
 <S:Body>
  <VehicleDescription country="US" language="en" modelYear="2008" bestMakeName="Audi" bestModelName="S4" bestStyleName="5dr Avant Wgn" xmlns="urn:description7b.services.chrome.com">
     <responseStatus responseCode="Successful" description="Successful"/>

     <style id="292015" modelYear="2008" name="5dr Avant Wgn Man" nameWoTrim="5dr Avant Wgn Man" mfrModelCode="8ED549" fleetOnly="false" modelFleet="false" passDoors="4" altBodyType="Station Wagon" drivetrain="All Wheel Drive">
        <division id="4">Audi</division>
        <subdivision id="5020">Audi</subdivision>
        <model id="17308">S4</model>
        <basePrice unknown="false" invoice="46137.0" msrp="49610.0" destination="775.0"/>
        <bodyType primary="true" id="7">Station Wagon</bodyType>
        <marketClass id="53">Small Wagon</marketClass>
        <acode>USB80AUC085A0</acode>
     </style>
     <style id="292016" modelYear="2008" name="5dr Avant Wgn Auto" nameWoTrim="5dr Avant Wgn Auto" mfrModelCode="8ED54L" fleetOnly="false" modelFleet="false" passDoors="4" altBodyType="Station Wagon" drivetrain="All Wheel Drive">
        <division id="4">Audi</division>
        <subdivision id="5020">Audi</subdivision>
        <model id="17308">S4</model>
        <basePrice unknown="false" invoice="47162.0" msrp="50710.0" destination="775.0"/>
        <bodyType primary="true" id="7">Station Wagon</bodyType>
        <marketClass id="53">Small Wagon</marketClass>
        <acode>USB80AUC085A1</acode>
     </style>
     <technicalSpecification>
        <titleId>1</titleId>
        <value value="Audi S4" condition="-PT">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
        <value value="Audi S4" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>2</titleId>
        <value value="5 Door Wagon" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>6</titleId>
        <value value="All-Wheel Drive" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>7</titleId>
        <value value="Small Station Wagon" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>8</titleId>
        <range min="5.0" max="5.0"/>
        <value value="5" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>9</titleId>
        <range min="90.8" max="90.8"/>
        <value value="90.8" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>10</titleId>
        <range min="4012.0" max="4101.0"/>
        <value value="4012" condition="">
           <styleId>292015</styleId>
        </value>
        <value value="4101" condition="">
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>26</titleId>
        <range min="13.0" max="14.0"/>
        <value value="13" condition="">
           <styleId>292015</styleId>
        </value>
        <value value="14" condition="">
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>27</titleId>
        <range min="20.0" max="21.0"/>
        <value value="20" condition="">
           <styleId>292015</styleId>
        </value>
        <value value="21" condition="">
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>31</titleId>
        <value value="- TBD -" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>32</titleId>
        <value value="- TBD -" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>33</titleId>
        <value value="- TBD -" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>34</titleId>
        <value value="- TBD -" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>40</titleId>
        <value value="" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>41</titleId>
        <value value="Gas V8" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>42</titleId>
        <value value="4.2L/254" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>43</titleId>
        <value value="SEFI" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>48</titleId>
        <value value="340 @ 6800" condition="">
           <styleId>292016</styleId>
        </value>
        <value value="340 @ 7000" condition="">
           <styleId>292015</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>49</titleId>
        <value value="302 @ 3500" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>51</titleId>
        <value value="" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>52</titleId>
        <range min="6.0" max="6.0"/>
        <value value="6" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>53</titleId>
        <value value="Manual" condition="">
           <styleId>292015</styleId>
        </value>
        <value value="Automatic" condition="">
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>54</titleId>
        <value value="" condition="">
           <styleId>292015</styleId>
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>56</titleId>
        <range min="3.67" max="4.17"/>
        <value value="3.67" condition="">
           <styleId>292015</styleId>
        </value>
        <value value="4.17" condition="">
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>57</titleId>
        <range min="2.05" max="2.34"/>
        <value value="2.05" condition="">
           <styleId>292015</styleId>
        </value>
        <value value="2.34" condition="">
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>58</titleId>
        <range min="1.46" max="1.52"/>
        <value value="1.46" condition="">
           <styleId>292015</styleId>
        </value>
        <value value="1.52" condition="">
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
     <technicalSpecification>
        <titleId>59</titleId>
        <range min="1.13" max="1.14"/>
        <value value="1.13" condition="">
           <styleId>292015</styleId>
        </value>
        <value value="1.14" condition="">
           <styleId>292016</styleId>
        </value>
     </technicalSpecification>
  </VehicleDescription>

还有我的PHP。该PHP将这两种样式类型放入一个下拉列表中,并打印<technicalSpecification>。我只需要根据在下拉列表中选择的<techhnicalSpecification>来更改style

<html>
 <body>

  <?php

   $xml = file_get_contents('data.xml');
   $dom = new DOMDocument();
   $dom->loadXML($xml);

  ?>

  <form>
   <select>
    <option selected="selected">Choose style</option>
   <?php
    foreach ($dom->getElementsByTagName('style') as $styleName){
      $styleType = $styleName->getAttribute("name");
      $styleArray[] = $styleType;
     }
    foreach ($styleArray as $style){
     ?>
     <option><?php echo $style;?> </option>
    <?php
     }
    ?>
    </select>
   </form>

   <br>

    <?php

    foreach ( $dom->getElementsByTagName('technicalSpecification') as $techSpec )   {
    $id = $techSpec->getElementsByTagName('titleId')->item(0)->nodeValue;
    $techData [$id]= $techSpec->getElementsByTagName('value')->item(0)->getAttribute("value")."<br>";
     }

    echo "<br><br>";
    print_r($techData);
   ?>

  </body>
 </html>

非常感谢!为您提供帮助!

1 个答案:

答案 0 :(得分:0)

那是SOAP(不仅仅是XML),因此您应该尝试使用PHP SOAP库。但是,如果您只想将其解析为XML,请查看DOMXpath::evaluate()。它允许您使用Xpath表达式来获取节点(请考虑使用SQL,但要使用XML DOM)。您可以根据属性值选择元素节点:

$xpath = new DOMXpath($document);
foreach ($xpath->evaluate('//technicalSpecification[value/@value="5dr Avant Wgn Man"]') as $specNode) {
   ...
}