2 回答

TA貢獻1851條經(jīng)驗 獲得超4個贊
您可以使用 DOM 和 XPath 表達式。所有數(shù)據(jù)都在{https://api.nhs.uk/data/services}organisationSummary節(jié)點內(nèi)。
下面的示例注冊service命名空間的前綴https://api.nhs.uk/data/services(這就是s示例中的 解析為)。然后它{https://api.nhs.uk/data/services}organisationSummary使用//service:organisationSummary表達式獲取任何節(jié)點。
對于每個組織,它$item使用標(biāo)量值的表達式構(gòu)建一個變量。循環(huán)用于{https://api.nhs.uk/data/services}addressLine節(jié)點。
// create the DOM document and load the XML
$document = new DOMDocument();
$document->loadXML($xml);
// create an Xpath instance for the document - register namespace
$xpath = new DOMXpath($document);
$xpath->registerNamespace('service', 'https://api.nhs.uk/data/services');
$json = [
"success" => true,
// force object for data - it will be an array otherwise
"data" => new stdClass()
];
// iterate over all organisationSummary nodes
foreach ($xpath->evaluate('//service:organisationSummary') as $index => $organisation) {
// fetch single values
$item = [
"name" => $xpath->evaluate('string(service:name)', $organisation),
"odscode" => $xpath->evaluate('string(service:odsCode)', $organisation),
"postcode" => $xpath->evaluate('string(service:address/service:postcode)', $organisation),
"telephone" => $xpath->evaluate('string(service:contact/service:telephone)', $organisation),
"longitude" => $xpath->evaluate('string(service:geographicCoordinates/service:longitude)', $organisation),
"latitude" => $xpath->evaluate('string(service:geographicCoordinates/service:latitude)', $organisation),
"distance" => $xpath->evaluate('string(service:Distance)', $organisation)
];
// add the addressLine values
foreach ($xpath->evaluate('service:address/service:addressLine', $organisation) as $lineIndex => $line) {
$item['addressline'.$lineIndex] = $line->textContent;
}
// add the $item
$json['data']->{$index} = $item;
}
echo json_encode($json, JSON_PRETTY_PRINT);
輸出:
{
"success": true,
"data": {
"0": {
"name": "Highfields Surgery (R Wadhwa)",
"odscode": "C82116",
"postcode": "LE2 0NN",
"telephone": "01162543253",
"longitude": "-1.11859357357025",
"latitude": "52.6293983459473",
"distance": "0.247038430918239",
"addressline0": "25 Severn Street",
"addressline1": "Leicester",
"addressline2": "Leicestershire"
},
"1": {
"name": "Dr R Kapur & Partners",
"odscode": "C82659",
"postcode": "LE2 0TA",
"telephone": "01162951258",
"longitude": "-1.11907768249512",
"latitude": "52.6310386657715",
"distance": "0.30219913005026",
"addressline0": "St Peters Health Centre",
"addressline1": "Sparkenhoe Street",
"addressline2": "Leicester",
"addressline3": "Leicestershire"
},
...

TA貢獻1877條經(jīng)驗 獲得超1個贊
您需要使用適當(dāng)?shù)姆椒▉肀闅v XML 文檔,而不是查看每個元素并檢查其名稱。
您的兩個最佳選擇是 DOM 方法和 XPath。如果您使用過前端 JavaScript 代碼(例如document.getElementsByTagName或document.getElementById),您就會熟悉前者。后者具有更陡峭的學(xué)習(xí)曲線,但功能要強大得多。(XSLT 也是轉(zhuǎn)換 XML 的好選擇,但我對它不是很熟悉。)
不過,第一步是使用正確的 XML 庫。您使用的 XML 函數(shù)級別非常低。我建議改用 PHP 的DomDocument擴展。讓我們潛入吧!
<?php
$xml = file_get_contents("nhs.xml");
// assume XML document is stored in a variable called $xml
$dom = new DomDocument;
$dom->loadXml($xml);
// thankfully we can ignore namespaces!
$summaries = $dom->getElementsByTagName("organisationSummary");
// go through each <organisationSummary> element
foreach ($summaries as $os) {
$entry_data = [
// note we're using $os and not $dom now, so we get child elements of this particular element
"name" => $os->getElementsByTagName("name")[0]->textContent,
"odscode" => $os->getElementsByTagName("odsCode")[0]->textContent,
"addressline0" => $os->getElementsByTagName("addressLine")[0]->textContent,
// provide a default empty string in case there's no further lines
"addressline1" => $os->getElementsByTagName("addressLine")[1]->textContent ?? "",
"addressline2" => $os->getElementsByTagName("addressLine")[2]->textContent ?? "",
"addressline3" => $os->getElementsByTagName("addressLine")[3]->textContent ?? "",
"postcode" => $os->getElementsByTagName("postcode")[0]->textContent,
"telephone" => $os->getElementsByTagName("telephone")[0]->textContent,
"longitude" => $os->getElementsByTagName("longitude")[0]->textContent,
"latitude" => $os->getElementsByTagName("latitude")[0]->textContent,
"distance" => $os->getElementsByTagName("Distance")[0]->textContent,
];
$json_data[] = $entry_data;
}
if (count($json_data)) {
$output = ["success" => true, "data" => $json_data];
} else {
$output = ["success" => false];
}
echo json_encode($output, JSON_PRETTY_PRINT);
- 2 回答
- 0 關(guān)注
- 192 瀏覽
添加回答
舉報