0

我正在尝试将 xml 文件输出到一个数组,然后输出到屏幕。xml 文件加载我知道它会加载,因为我可以输出 entry > Id 但我无法访问它的子节点。我需要位于的数据。

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

我将如何访问位于 s:organisationSummay ,s:address, s:geographicCoordinates 中的数据,以便我可以为该子节点中的每个项目获取ElementsByTagName。

$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>webservices@nhschoices.nhs.uk</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>
4

1 回答 1

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;
}

但是,请考虑使用DOMXPathor SimpleXML,因为它们比 dom 遍历更容易。

于 2012-07-11T09:53:33.880 回答