【问题标题】:Missing items using simpleXML in PHP在 PHP 中使用 simpleXML 丢失项目
【发布时间】:2011-12-06 00:34:24
【问题描述】:

我有这个 XML 输出。我将它输入 PHP 中的 simpleXML。我正在尝试检索所有的立场、地块和树木。出于某种原因,我只得到第一站的情节,而不是第二站的情节。树木也是如此,我只是在第一个支架上把树弄回来。我正在使用的代码发布在 XML 下方。我尝试了许多不同的方法来点击这些部分,但没有运气。

 <Tracts>
 <tract>
  <tract>tract1</tract> 
 <county>Glynn</county> 
<state>GA</state> 
<name>Garrett</name> 
<owner>Bob</owner> 
<date>Nov 6, 2011</date> 
 <stands>
 <Stand>
 <stand>1</stand> 
  <age>12</age> 
  <thinned>true</thinned> 
  <thinYear>2007</thinYear> 
  <species>PL Lob</species> 
 <cruiser>me</cruiser> 
<hPlotSize>50th Acre</hPlotSize> 
<pPlotSize>50th Acre</pPlotSize> 
<treeType>None</treeType> 
<fixedOrPrism>5</fixedOrPrism> 
<plots>
<Plot>
 <plotNum>5</plotNum> 
 <pPMStems>12</pPMStems> 
 <hPMStems>12</hPMStems> 
 <pPMHt>12</pPMHt> 
 <hPMHt>12</hPMHt> 
 <pMStems>12</pMStems> 
 <hMStems>12</hMStems> 
  <pMHt>0</pMHt> 
 <hMHt>12</hMHt> 
 <pHtCrown>0</pHtCrown> 
<trees>
<Tree>
<treeNum>3</treeNum> 
<DBH>18</DBH> 
<species>LOB</species> 
<type>CROP</type> 
<merch>12</merch> 
<htToCrown>9</htToCrown> 
<merchLogs>1.0</merchLogs> 
<defects>
<string>CRON</string> 
<string>SCRAPE</string> 
</defects>
</Tree>
<Tree>
 <treeNum>3</treeNum> 
 <DBH>18</DBH> 
 <species>LOB</species> 
 <type>CROP</type> 
<merch>12</merch> 
<htToCrown>9</htToCrown> 
<merchLogs>1.0</merchLogs> 
<defects>
  <string>CRON</string> 
<string>SCRAPE</string> 
</defects>
</Tree>
<Tree>
<treeNum>3</treeNum> 
<DBH>18</DBH> 
<species>LOB</species> 
<type>CROP</type> 
<merch>12</merch> 
<htToCrown>9</htToCrown> 
<merchLogs>1.0</merchLogs> 
<defects>
<string>CRON</string> 
<string>SCRAPE</string> 
</defects>
</Tree>
</trees>
</Plot>
<Plot>
<plotNum>9</plotNum> 
<pPMStems>3</pPMStems> 
<hPMStems>3</hPMStems> 
<pPMHt>3</pPMHt> 
<hPMHt>3</hPMHt> 
<pMStems>3</pMStems> 
<hMStems>3</hMStems> 
<pMHt>0</pMHt> 
<hMHt>3</hMHt> 
<pHtCrown>0</pHtCrown> 
<trees /> 
 </Plot>
</plots>
</Stand>
<Stand>
 <stand>2</stand> 
<age>20</age> 
<thinned>false</thinned> 
<thinYear>0</thinYear> 
 <species>PL Lob</species> 
 <cruiser>me</cruiser> 
<hPlotSize>50th Acre</hPlotSize> 
<pPlotSize>10th Acre</pPlotSize> 
<treeType>Fixed</treeType> 
<fixedOrPrism>100%</fixedOrPrism> 
<plots>
<Plot>
<plotNum>2</plotNum> 
<pPMStems>12</pPMStems> 
<hPMStems>20</hPMStems> 
<pPMHt>32</pPMHt> 
<hPMHt>16</hPMHt> 
<pMStems>21</pMStems> 
<hMStems>7</hMStems> 
<pMHt>0</pMHt> 
<hMHt>13</hMHt> 
<pHtCrown>0</pHtCrown> 
<trees>
<Tree>
  <treeNum>1</treeNum> 
  <DBH>10</DBH> 
  <species>LOB</species> 
   <type>CROP</type> 
  <merch>12</merch> 
  <htToCrown>16</htToCrown> 
  <merchLogs>4.5</merchLogs> 
 <defects>
  <string>CRON</string> 
   <string>OTHER</string> 
</defects>
 </Tree>
 </trees>
 </Plot>
</plots>
 </Stand>
 </stands>
 </tract>
</Tracts>

  foreach($xml->tract->stands->Stand->plots->Plot as $plot)
  {
  echo $plot->plotNum;
  }
  echo '<br><br>'; 

  foreach ($xml->tract->stands->Stand->plots->Plot->trees->Tree as $tree) 
  {
  echo $tree->treeNum;
  }

【问题讨论】:

    标签: php simplexml


    【解决方案1】:

    变量 $xml-&gt;tract-&gt;stands-&gt;Stand-&gt;plots-&gt;Plot 只会引用第一个 Stand 元素 - 将文档视为一组嵌套数组。

    SimpleXML 假定如果您提到一个元素名称而不循环或索引到它,您需要该名称的第一个元素。也就是说,和写foreach ( $xml-&gt;tract[0]-&gt;stands[0]-&gt;Stand[0]-&gt;plots[0]-&gt;Plot ) ...是一样的

    所以你需要嵌套你的 foreach 循环:

    foreach($xml->tract->stands->Stand as $stand)
    {
       foreach($stand->plots->Plot as $plot)
       {
           echo $plot->plotNum;
    
           foreach ( $plot->trees->Tree as $tree )
           {
               echo $tree->treeNum;
           }
       }
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多