我有以下XML文档:
<RESPONSE>
<SUCCESS>true</SUCCESS>
<ERRORMESSAGE></ERRORMESSAGE>
<TIMESTAMP>21/01/2015 10:37:22</TIMESTAMP>
<AUDITID>1285153274</AUDITID>
<LOOKUP_VRM>DE14YWK</LOOKUP_VRM>
<LOOKUP_VIN/>
<MONTHLYLOOKUPLIMITEXCEEDED>False</MONTHLYLOOKUPLIMITEXCEEDED>
<AUTOCORRECTED>False</AUTOCORRECTED>
<AUTOCORRECTEDREASON></AUTOCORRECTEDREASON>
<MATCHLEVEL>...</MATCHLEVEL>
<DATA>
<DVLA>
<VRM>DE14YWK</VRM>
<VIN>LBPRE054000006883</VIN>
<MANUFACTURER>YAMAHA</MANUFACTURER>
<MODEL>YBR 125</MODEL>
<FUELTYPE>PETROL</FUELTYPE>
<BODYTYPE>MOTORCYCLE</BODYTYPE>
<DOORS></DOORS>
<SEATING>2</SEATING>
<WHEELPLAN>2 WHEEL</WHEELPLAN>
<REGISTRATIONDATE>20140411</REGISTRATIONDATE>
<MANUFACTUREDDATE>20140411</MANUFACTUREDDATE>
<FIRSTREG_DATE>20140411</FIRSTREG_DATE>
<FIRSTREG_USEDBEFORE>0</FIRSTREG_USEDBEFORE>
<CO2>0</CO2>
<ENGINECAPACITY>125</ENGINECAPACITY>
<ENGINENUMBER>E399E0121734</ENGINENUMBER>
<ISSCRAPPED>No</ISSCRAPPED>
<SCRAPPEDDATE></SCRAPPEDDATE>
<ISEXPORTED>No</ISEXPORTED>
<EXPORTEDDATE></EXPORTEDDATE>
<ISIMPORTED>No</ISIMPORTED>
<IMPORTEDDATE></IMPORTEDDATE>
<COLOUR>RED</COLOUR>
<MASSINSERVICE>126</MASSINSERVICE>
<MAXMASS>0</MAXMASS>
<MAXNETPOWER>7</MAXNETPOWER>
<MAXTOW_BRAKED>0</MAXTOW_BRAKED>
<MAXTOW_UNBRAKED>0</MAXTOW_UNBRAKED>
<SOUNDLEVEL_STATIONARY>78</SOUNDLEVEL_STATIONARY>
<SOUNDLEVEL_ENGINE>3900</SOUNDLEVEL_ENGINE>
<SOUNDLEVEL_DRIVEBY>74</SOUNDLEVEL_DRIVEBY>
<PREVIOUSAQUISITION>20140411</PREVIOUSAQUISITION>
<PREVIOUSDISPOSAL></PREVIOUSDISPOSAL>
<PREVIOUSKEEPERS>0</PREVIOUSKEEPERS>
</DVLA>
</DATA>
</RESPONSE>
当我运行以下代码时,它将XML表格中的所有细节都提供给一个居中的列表,只显示标签的内部。这是代码:
<?php
/**
* PHP XML Parser
* Map XML to HTML
*
*/
$file = "http://webservices.capnetwork.co.uk/capdvla_webservice/capdvla.asmx/DVLALookupVRM?SubscriberID=171399&Password=PHA399&vrm=DE14YWK";
$map_array = array(
"BOLD" => "B",
"EMPHASIS" => "I",
"LITERAL" => "TT"
);
echo "<center>";
//setting the first callable function needed
//by xml_set_element_handler
function startElement($parser, $name, $attrs) {
global $map_array;
if (isset($map_array[$name])) {
echo "<$map_array[$name]>";
}
}
//setting the second callable function needed
//by xml_set_element_handler
function endElement($parser, $name) {
global $map_array;
if (isset($map_array[$name])) {
echo "</$map_array[$name]>";
}
}
function characterData($parser, $data) {
echo $data . '<br />';
}
//creating a new xml parser
$xml_parser = xml_parser_create();
// use case-folding so we are sure to find the tag in $map_array
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
//if the xml can't be open then stop the script
if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}
while ($data = fread($fp, 4096)) {
//opening the xml and parsing the elements
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
?>
它显示外部XML文档的每个细节。我希望它只显示&lt; DVLA&gt;物品&lt; VRM&gt;&lt;制造商&gt;&lt; MODEL&gt;&lt;颜色&gt;而不是从质疑的XML表中提取所有项目。
感谢阅读!
答案 0 :(得分:0)
如果XML的结构始终相同,您可以简单地迭代:
$xml = simplexml_load_string($x); // assume XML in $x
foreach ($xml->DATA->DVLA as $d) {
echo $d->VRM . PHP_EOL;
echo $d->MANUFACTURER . PHP_EOL;
echo $d->MODEL . PHP_EOL;
echo "--------------------" . PHP_EOL;
}
如果XML包含多个<DVLA>
,则会全部列出。
看到它正常工作:https://eval.in/246291