2012-07-09 108 views
0

我想输出一个xml文件,然后输出到屏幕的数组。 XML文件加载我知道它加载,因为我可以输出条目> ID但我无法访问其子节点。我需要位于数据PHP输出嵌套的孩子节点

content > s:organisationSummay 
content > s:organisationSummay > s:address 
content > s:organisationSummay > s:geographicCoordinates 

我如何将访问位于S中的数据:organisationSummay,S:地址,S:geographicCoordinates这样我就可以getElementsByTagName来在子节点的每个项目。

$doc2 = new DOMDocument(); 
$url = 'http://v1.syndication.nhschoices.nhs.uk/organisations/'.$_POST['ServiceType'].'/postcode/'.$_POST['PostCode'].'.xml?apikey=??&range=50'; 
echo $url; 

$doc2->load($url); 
$arrFeeds = array(); 

foreach ($doc2->getElementsByTagName('entry') as $node) 
{ 
    echo $node->getElementsByTagName($content->'s:name'); 

    $itemRSS = array (
    'PracticeName' => $organisationSummary->getElementsByTagName('s:name')->item(0)->nodeValue 
); 

    array_push($arrFeeds, $itemRSS); 
} 
<?xml version="1.0" encoding="utf-8"?> 
<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 - ls1- Within 50km</title> 
    <id>http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/postcode/bd164jt?range=50</id> 
    <rights type="text">© Crown Copyright 2009</rights> 
    <updated>2012-07-06T10:24:46+01:00</updated> 
    <category term="Search"/> 
    <logo>http://www.nhs.uk/nhscwebservices/documents/logo1.jpg</logo> 
    <author> 
    <name>NHS Choices</name> 
    <uri>http://www.nhs.uk</uri> 
    <email>[email protected]</email> 
    </author> 
    <link rel="self" type="application/xml" title="NHS Choices - GP Practices Near Postcode - ;ls1 - Within 50km" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/postcode/ls1?apikey=??&amp;range=50"/> 
    <link rel="first" type="application/xml" title="first" length="1000" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/postcode/ls1?apikey=??&amp;range=50&amp;page=1"/> 
    <link rel="next" type="application/xml" title="next" length="1000" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/postcode/Ls1?apikey=??&amp;range=50&amp;page=2"/> 
    <link rel="last" type="application/xml" title="last" length="1000" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/postcode/LS1?apikey=??&amp;range=50&amp;page=10"/> 
    <link rel="alternate" title="NHS Choices - Find and choose services - GP Practices" href="http://www.nhs.uk/ServiceDirectories/pages/ServiceSearch.aspx?ServiceType=GP"/> 
    <s:SearchCoords>439300,411100</s:SearchCoords> 
    <entry> 
    <id>http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/1</id> 
    <title type="text">Medical Practice</title> 
    <updated>2012-07-06T09:24:46Z</updated> 
    <link rel="self" title="Medical Practice" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/1?apikey=??"/> 
    <link rel="alternate" title="Medical Practice" href="http://www.nhs.uk/ServiceDirectories/Pages/GP.aspx?pid=1"/> 
    <content type="application/xml"> 
     <s:organisationSummary> 
     <s:name>Medical Practice</s:name> 
     <s:address> 
      <s:addressLine>Health Care Centre</s:addressLine> 
      <s:addressLine>2</s:addressLine> 
      <s:addressLine>Town</s:addressLine> 
      <s:addressLine>Yorkshire</s:addressLine> 
      <s:postcode>?</s:postcode> 
     </s:address> 
     <s:contact type="General"> 
      <s:telephone>5558383</s:telephone> 
     </s:contact> 
     <s:geographicCoordinates> 
      <s:northing>438880</s:northing> 
      <s:easting>411444</s:easting> 
      <s:longitude>-1.82821202227791</s:longitude> 
      <s:latitude>53.996218047559</s:latitude> 
     </s:geographicCoordinates> 
     <s:Distance>0.5</s:Distance> 
     </s:organisationSummary> 
    </content> 
    </entry> 
    <entry> 
    <id>http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/2</id> 
    <title type="text">Surgery</title> 
    <updated>2012-07-06T09:24:46Z</updated> 
    <link rel="self" title="Surgery" href="http://v1.syndication.nhschoices.nhs.uk/organisations/gppractices/1?apikey=??"/> 
    <link rel="alternate" title="Surgery" href="http://www.nhs.uk/ServiceDirectories/Pages/GP.aspx?pid=2"/> 
    <content type="application/xml"> 
     <s:organisationSummary> 
     <s:name>Surgery</s:name> 
     <s:address> 
      <s:addressLine>Healthcare Centre</s:addressLine> 
      <s:addressLine>Kings</s:addressLine> 
      <s:addressLine>Town</s:addressLine> 
      <s:postcode>?</s:postcode> 
     </s:address> 
     <s:contact type="General"> 
      <s:telephone>555555</s:telephone> 
      <s:email>Email</s:email> 
     </s:contact> 
     <s:geographicCoordinates> 
      <s:northing>78421</s:northing> 
      <s:easting>484100</s:easting> 
      <s:longitude>-1.828987402220691</s:longitude> 
      <s:latitude>53.987218047559</s:latitude> 
     </s:geographicCoordinates> 
     <s:Distance>0.5</s:Distance> 
     </s:organisationSummary> 
    </content> 
    </entry> 


</feed> 

回答

1

这是一个命名空间的文件,所以您需要使用正确的命名空间的方法,例如DOMDocument::getElementsByTagNameNS

另外,你的循环有太多的错误,我怀疑你要么不包括所有的代码,要么你真的误解了DOMDocument的工作原理。

$NS = array(
    's' => "http://syndication.nhschoices.nhs.uk/services", 
    'atom' => "http://www.w3.org/2005/Atom", 
); 

$entries = array(); 

foreach ($doc2->getElementsByTagNameNS($NS['s'], 'organisationSummary') as $node) 
{ 
    $entries[] = array(
     'name' => trim($node->getElementsByTagNameNS($NS['s'], 'name')->item(0)->textContent), 
     'address' => keyByElementName($node->getElementsByTagNameNS($NS['s'], 'address')->item(0)), 
     'geographicCoordinates' => keyByElementName($node->getElementsByTagNameNS($NS['s'], 'geographicCoordinates')->item(0)), 
    ); 
} 

function keyByElementName(DOMNode $node) 
{ 
    $elem = array(); 
    foreach ($node->childNodes as $child) { 
     if ($child->nodeType===XML_ELEMENT_NODE) { 
      $elem[$child->localName] = trim($child->textContent); 
     } 
    } 
    return $elem; 
} 

但是,考虑使用DOMXPathSimpleXML,因为这些会比DOM遍历更加容易。