0
function loadFilesToArrays(){
    $notpromo1Array = simplexml_load_file('pages/hegnar/source/1/notpromo-1_08_04_2013_1.xml');
        foreach($notpromo1Array as $xml){
            echo $xml -> getName();
            echo "<br>";
            echo $xml -> ORDREHODE -> SluttkundeNr;
            echo "<br>";
        }
}

My XML look like this

<?xml version="1.0" encoding="UTF-8"?>
<IS_DATA>
<ORDRER class="linked-list">
    <ORDREHODE>
        <ORDREKUNDENR>10541</ORDREKUNDENR>
        <SluttkundeNr>1240</SluttkundeNr>
                    <AND OTHER PROPERTIES></AND OTHER PROPERTIES>..........
    </ORDREHODE>
    <ORDREHODE>
        <ORDREKUNDENR>10541</ORDREKUNDENR>
        <SluttkundeNr>1344</SluttkundeNr>
                    <AND OTHER PROPERTIES></AND OTHER PROPERTIES>..........
    </ORDREHODE>
    <ORDREHODE>
        ETC ETC ETC ..................
    </ORDREHODE>
</ORDRER>
</IS_DATA>

And the XML is properly ended, etc, I used notepad++'s xml validator.

I don't understand why when I call loadFilesToArray function, I get this error Notice: Trying to get property of non-object in /path/to/file/page1.php on line 104 The IS_DATA is after all enclosed in curly brackets and single quotes as I have seen lots of people referring to when wanting to echo data from XML having special characters in property names in XML.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Shogoot
  • 297
  • 2
  • 6
  • 13
  • `$xml` already represents `` or even ``. Also please see [the basic simplexml examples](http://php.net/simplexml.examples-basic) which show that nicely and [for error messages we've got a good reference here on SO](http://stackoverflow.com/q/12769982/367456). – hakre Apr 18 '13 at 10:06
  • can you tell me what is exactly on line number 104 ?? – Ravindra Shekhawat Apr 18 '13 at 10:21
  • @RavindraShekhawat foreach($notpromo1Array as $xml){ – Shogoot Apr 18 '13 at 10:40
  • @Shogoot add a print_r($notpromo1Array); instruction right before the foreach, what does it show? – Rolando Isidoro Apr 18 '13 at 11:23

3 Answers3

1

Please give correct path of your xml file right now a.xml and test.php both in same folder

a.xml

<?xml version="1.0" encoding="UTF-8"?>
<IS_DATA>
<ORDRER class="linked-list">
    <ORDREHODE>
        <ORDREKUNDENR>10541</ORDREKUNDENR>
        <SluttkundeNr>1240</SluttkundeNr>
                    <ANDOTHERPROPERTIES></ANDOTHERPROPERTIES>
    </ORDREHODE>
    <ORDREHODE>
        <ORDREKUNDENR>10541</ORDREKUNDENR>
        <SluttkundeNr>1344</SluttkundeNr>
                    <ANDOTHERPROPERTIES></ANDOTHERPROPERTIES>
    </ORDREHODE>
    <ORDREHODE>
        ETC ETC ETC ..................
    </ORDREHODE>
</ORDRER>
</IS_DATA>

test.php

    <?php
$notpromo1Array = simplexml_load_file("a.xml");
foreach($notpromo1Array as $xml)
  {
    foreach($xml as $child)
    {
        echo $child->getName();
        echo "<br>";
        echo $child->SluttkundeNr;
        echo "<br>";
    } 
  }

 /*RESULT
  * ORDREHODE
    1240
    ORDREHODE
    1344
    ORDREHODE
  */

?> 
Ravindra Shekhawat
  • 4,275
  • 1
  • 19
  • 26
1

You need to debug your code:

foreach ($notpromo1Array as $xml)
{
    echo $xml->getName(), "\n";
}

This gives you the name of the element represented by $xml:

ORDRER

As this element does not have any <IS_DATA> child, simplexml gives you NULL. And then you access ->ORDRER on NULL which does not work as it is not an object:

Notice: Trying to get property of non-object

That simple it is. Just access the correct elements and you're fine:

foreach ($notpromo1Array as $xml)
{
    echo $xml->getName(), "\n";
    echo $xml->ORDREHODE->SluttkundeNr, "\n";
}

Output:

ORDRER
1240
hakre
  • 193,403
  • 52
  • 435
  • 836
  • I did as you suggested and your right on the output, strange thing though, my function is not looping. Look at my output: http://imageshack.us/content_round.php?page=done&l=img94/3492/notlooping.png – Shogoot Apr 18 '13 at 11:01
  • You're really starting with this I guess. Check the basic simplexML examples I've linked in the comment directly under your question, it has very nice examples and you step away from your own code which often gives a better view on things as well. You need to specify which elements you want to loop over, e.g. `$xml->ORDERHODE as $orderhode`. Simplexml does not know by reading your brain ;) – hakre Apr 18 '13 at 11:46
0

just made a quick test and here's what I came up with. Replace your foreach code for this one:

foreach($notpromo1Array as $xml){
   echo $xml->ORDREHODE->SluttkundeNr;
   echo "<br />";
}

FYI, I used the following XML file as example and it printed 1240:

<?xml version="1.0" encoding="UTF-8"?>  
<IS_DATA>
    <ORDRER class="linked-list">
        <ORDREHODE>
            <ORDREKUNDENR>10541</ORDREKUNDENR>
            <SluttkundeNr>1240</SluttkundeNr>
        </ORDREHODE>
    </ORDRER>
</IS_DATA>
Rolando Isidoro
  • 4,983
  • 2
  • 31
  • 43