2011-12-06 89 views
1

我有這個XML輸出。我在PHP中將它提供給simpleXML。我試圖找回所有的看臺,地塊和樹木。出於某種原因,我只是在第一個展位而不是第二個展位獲得這些地塊。樹木也是一樣,我只是把樹倒在第一架。我使用的代碼在XML下面發佈。我嘗試了許多不同的方式來擊中這些部分,但沒有運氣。在PHP中使用simpleXML缺少項目

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

回答

0

可變$xml->tract->stands->Stand->plots->Plot將只引用第一Stand元件 - 以爲文檔作爲一組嵌套陣列的。

SimpleXML假定如果您提到一個沒有循環或編入索引的元素名稱,則需要該名稱的第一個元素。換句話說,它是foreach ($xml->tract[0]->stands[0]->Stand[0]->plots[0]->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; 
     } 
    } 

}