1

I have a xml that looks like this

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

I load this from a file that contais many ArticleId-elements using simplexml_load_file().

The problem is that I don't get all of the info for StockQuant. If I print_r the xml, then I get the data; 1.0, but I don't get the attributes; storeId="72". If I do it like the example belove, then I only get 1 StockQuant element, not both.

I iterate through the elements like this:

foreach( $xml as $key => $value )
{
    foreach( $value->StockQuant as $key2 => $value2 )
    {
        $stocks = simplexml_import_dom($value2);
        print_r($stocks);
    }
}

*Edit to add the foreach loop.

johnohod
  • 494
  • 5
  • 19

2 Answers2

0

Try below code:

foreach( $xml as $key => $value )
{
    foreach( $value->StockQuant as $key2 => $value2 )
    {
         echo $value2['storeId']. "\n" ;         
    }
} 
Shailesh Chauhan
  • 559
  • 3
  • 14
  • 1
    I should have said. I iterate through all the ArticleId-elements with a foreach loop. And then I can't reference StockQuant in that way. The closest I have come is something like this: `foreach( $value->StockQuant as $key2 => $value2 ) { $stocks = simplexml_import_dom($value2); print_r($stocks); }` It gives me all the data for one node, but I don't get more than one node. – johnohod Sep 12 '16 at 09:45
  • See link https://eval.in/640219 – Shailesh Chauhan Sep 12 '16 at 09:50
  • Close, but could you get the same output if you iterate through $value (in your example) and not $value->StockQuant? – johnohod Sep 12 '16 at 09:57
  • Ignore above comment, this seems to be working perfectly. I wonder what I did wrong... – johnohod Sep 12 '16 at 10:04
-1

You can do this:

$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/>';
    }
}

Which outputs:

1.0 72
1.0 Utstilling
3.0 12
1.0 Utstilling
6.0 13
1.0 Utstilling

XML looks like that:

<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>
Philipp Palmtag
  • 1,310
  • 2
  • 16
  • 18