Shogoot
Shogoot

Reputation: 297

PHP looping through XML error: Trying to get property of non-object

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.

Upvotes: 0

Views: 127

Answers (3)

Ravindra Shekhawat
Ravindra Shekhawat

Reputation: 4353

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

?> 

Upvotes: 1

hakre
hakre

Reputation: 197767

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

Upvotes: 1

Rolando Isidoro
Rolando Isidoro

Reputation: 5114

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>

Upvotes: 0

Related Questions