我有一个看起来像这样的xml
<ArticleId keyId="3d5c0332:1533c106ef9:67eb" price="6495,00" primId="HP229141500">
<StockQuant storeId="72">1.0</StockQuant>
<StockQuant storeId="Utstilling">1.0</StockQuant>
<PosterName></PosterName>
<PosterDescription></PosterDescription>
<Dimension></Dimension>
<Assembled></Assembled>
<AssemblyPrice></AssemblyPrice>
</ArticleId>
我使用simplexml_load_file()从包含许多ArticleId元素的文件加载它。
问题是我没有获得StockQuant的所有信息。如果我print_r xml,那么我得到数据; 1.0,但我没有得到属性; STOREID =&#34; 72&#34 ;.如果我像示例belove那样做,那么我只获得1个StockQuant元素,而不是两者。
我遍历这样的元素:
foreach( $xml as $key => $value )
{
foreach( $value->StockQuant as $key2 => $value2 )
{
$stocks = simplexml_import_dom($value2);
print_r($stocks);
}
}
*编辑以添加foreach循环。
答案 0 :(得分:0)
尝试以下代码:
foreach( $xml as $key => $value )
{
foreach( $value->StockQuant as $key2 => $value2 )
{
echo $value2['storeId']. "\n" ;
}
}
答案 1 :(得分:-1)
你可以这样做:
$xml = simplexml_load_file('file.xml');
foreach($xml as $article )
{
// there are multiple quantities
foreach($article->StockQuant as $quantity)
{
echo $quantity[0];
echo ' ';
echo $quantity->attributes()->storeId;
echo '<br/>';
}
}
哪个输出:
1.0 72
1.0 Utstilling
3.0 12
1.0 Utstilling
6.0 13
1.0 Utstilling
XML看起来像这样:
<Articles>
<ArticleId keyId="3d5c0332:1533c106ef9:67eb" price="6495,00" primId="HP229141500">
<StockQuant storeId="72">1.0</StockQuant>
<StockQuant storeId="Utstilling">1.0</StockQuant>
<PosterName></PosterName>
<PosterDescription></PosterDescription>
<Dimension></Dimension>
<Assembled></Assembled>
<AssemblyPrice></AssemblyPrice>
</ArticleId>
<ArticleId keyId="3d5c0332:1533c106ef9:67eb" price="6495,00" primId="HP229141500">
<StockQuant storeId="12">3.0</StockQuant>
<StockQuant storeId="Utstilling">1.0</StockQuant>
<PosterName></PosterName>
<PosterDescription></PosterDescription>
<Dimension></Dimension>
<Assembled></Assembled>
<AssemblyPrice></AssemblyPrice>
</ArticleId>
<ArticleId keyId="3d5c0332:1533c106ef9:67eb" price="6495,00" primId="HP229141500">
<StockQuant storeId="13">6.0</StockQuant>
<StockQuant storeId="Utstilling">1.0</StockQuant>
<PosterName></PosterName>
<PosterDescription></PosterDescription>
<Dimension></Dimension>
<Assembled></Assembled>
<AssemblyPrice></AssemblyPrice>
</ArticleId>
</Articles>