我有这个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;
}
答案 0 :(得分: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;
}
}
}