2011-12-06 16 views
1

私はこのXML出力を持っています。 PHPでsimpleXMLに入力しています。私はすべてのスタンド、プロット、ツリーを取得しようとしています。何らかの理由で、私は最初のスタンドでプロットを取得しているだけで、2番目のスタンドではありません。木の場合も同じですが、私は木を最初の台に戻すだけです。私が使用しているコードは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->stands->Stand as $stand) 
{ 
    foreach($stand->plots->Plot as $plot) 
    { 
     echo $plot->plotNum; 

     foreach ($plot->trees->Tree as $tree) 
     { 
      echo $tree->treeNum; 
     } 
    } 

}

:言い換えれば、それはあなたがあなたのforeach文巣に必要 foreach ($xml->tract[0]->stands[0]->Stand[0]->plots[0]->Plot) ...

書き込みと同じです

関連する問題