在 PHP 中使用 simpleXML 丢失项目
我有这个 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;
}
I have this XML output. I am feeding it into simpleXML in PHP. I am trying to retrieve all stands, plots, and trees. For some reason, I am only getting the plots in the first stand and not the second. The same for trees, I am only getting the tree backs in the first stand. The code that I am using is posted below the XML. I have tried many different ways to hit these sections and no luck .
<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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
变量
$xml->tract->stands->Stand->plots->Plot
将仅引用第一个Stand
元素 - 想想文档作为一组嵌套数组。SimpleXML 假设如果您提及一个元素名称而不对其进行循环或索引,则您需要该名称的第一个元素。换句话说,它与编写
foreach ( $xml->tract[0]->stands[0]->Stand[0]->plots[0]->Plot ) 相同。 ..
所以你需要嵌套你的 foreach 循环:
}
The variable
$xml->tract->stands->Stand->plots->Plot
will only ever reference the firstStand
element - think of the document as a set of nested arrays.SimpleXML assumes that if you mention an element name without looping or indexing into it, you want the first element of that name. In other words, it's the same as writing
foreach ( $xml->tract[0]->stands[0]->Stand[0]->plots[0]->Plot ) ...
So you need to nest your foreach loops:
}