2011-08-20 115 views
1

我一直试图使用不同的方法来访问NHS API来读取XML。在PHP中使用XPath工作的问题

这里是XML的一个片段:

<feed xmlns:s="http://syndication.nhschoices.nhs.uk/services" xmlns="http://www.w3.org/2005/Atom"> 
<title type="text">NHS Choices - GP Practices Near Postcode - W1T4LB - Within 5km</title> 
<entry> 
    <id>http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/27369</id> 
    <title type="text">Fitzrovia Medical Centre</title> 
    <updated>2011-08-20T22:47:39Z</updated> 
    <link rel="self" title="Fitzrovia Medical Centre" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/27369?apikey="/> 
    <link rel="alternate" title="Fitzrovia Medical Centre" href="http://www.nhs.uk/ServiceDirectories/Pages/GP.aspx?pid=303A92EF-EC8D-496B-B9CD-E6D836D13BA2"/> 
    <content type="application/xml"> 
    <s:organisationSummary> 
    <s:name>Fitzrovia Medical Centre</s:name> 
    <s:address> 
    <s:addressLine>31 Fitzroy Square</s:addressLine> 
    <s:addressLine>London</s:addressLine> 
    <s:postcode>W1T6EU</s:postcode> 
    </s:address> 
    <s:contact type="General"> 
    <s:telephone>020 7387 5798</s:telephone> 
    </s:contact> 
    <s:geographicCoordinates> 
    <s:northing>182000</s:northing> 
    <s:easting>529000</s:easting> 
    <s:longitude>-0.140267259415255</s:longitude> 
    <s:latitude>51.5224357586293</s:latitude> 
    </s:geographicCoordinates> 
    <s:Distance>0.360555127546399</s:Distance> 
    </s:organisationSummary> 
    </content> 
</entry> 
</feed> 

我一直在使用这个PHP访问它:

<?php 

$feedURL = 'http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/postcode/W1T4LB.xml?apikey=&range=5'; 

$raw = file_get_contents($feedURL); 

$dom = new DOMDocument(); 
$dom->loadXML($raw); 

$xp = new DOMXPath($dom); 
$result = $xp->query("//entry"); // select all entry nodes 

print $result->item(0)->nodeValue; 

?> 

问题是我有没有效果,$raw数据存在,但$dom永远不会被字符串XML填充。这意味着XPath将不起作用。

另外...对于奖励积分:如何在本例中使用XPath访问<s:Name>标签?

一如既往地欣赏帮助。

编辑:

这里是工作的罚款所产生的PHP,这要归功于@Andrej大号

<?php 

$feedURL = 'http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/postcode/W1T4LB.xml?apikey=&range=5'; 
$xml = simplexml_load_file($feedURL); 
$xml->registerXPathNamespace('s', 'http://syndication.nhschoices.nhs.uk/services'); 
$result = $xml->xpath('//s:name'); 

foreach ($result as $title) 
{ 
    print $title . '<br />';  
} 

?> 

回答