簡體   English   中英

PHP輸出嵌套的子節點

[英]PHP outputing nested childnodes

我正在嘗試將xml文件輸出到隨后輸出到屏幕的數組。 xml文件已加載,我知道它已加載,因為我可以輸出entry> Id,但無法訪問其子節點。 我需要其中的數據。

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

我將如何訪問位於s:organisationSummay,s:address,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>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>

這是一個命名空間文檔,因此您需要使用適當的命名空間方法,例如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遍歷更容易。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM