Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0

    Trying to get property of non-object


    Data:

    <NewDataSet>
    <Table>
    <CATID>0</CATID>
    <CATDES>UNASSIGNED </CATDES>
    <ATTR1>locked </ATTR1>
    <ATTR2 xml:space="preserve"> </ATTR2>
    <ATTR3 xml:space="preserve"> </ATTR3>
    <ATTR4 xml:space="preserve"> </ATTR4>
    <ATTR5 xml:space="preserve"> </ATTR5>
    <ATTR6 xml:space="preserve"> </ATTR6>
    <ATTR7 xml:space="preserve"> </ATTR7>
    <ATTR8 xml:space="preserve"> </ATTR8>
    <ATTR9 xml:space="preserve"> </ATTR9>
    <ATTR0 xml:space="preserve"> </ATTR0>
    </Table>
    <Table>
    <CATID>1</CATID>
    <CATDES>ACCESSORIES MISCELLANEOUS </CATDES>
    <ATTR1>Type </ATTR1>
    <ATTR2>Model or Style </ATTR2>
    <ATTR3>Color </ATTR3>
    <ATTR4>Size </ATTR4>
    <ATTR5>Material </ATTR5>
    <ATTR6>Gauge </ATTR6>
    <ATTR7>Mount </ATTR7>
    <ATTR8>Battery Config </ATTR8>
    <ATTR9>Height </ATTR9>
    <ATTR0>Model Fit </ATTR0>
    </Table>

    Code:

    $URL_VALUE="URL_DATA _HERE;
    $xml=simplexml_load_file($URL_VALUE);
    $html=$xml->Table[1]->CATDES;
    echo $html;



    Returns Error

    Notice: Trying to get property of non-object in D:\apps\xampp\htdocs\practice\Playing.php on line 21

    Line 21:

    $html=$xml->Table[1]->CATDES;


    I was hoping to echo

    ACCESSORIES MISCELLANEOUS



    Any comments?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,392
    Rep Power
    594
    That is not the way Simple XML works. You need to read the documentation.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Sir, thank you for your comment. I have read documentation, watched videos, and searched this forum for the answer before posting and the code looked ok to me. I am not sure where I am making the mistake. Admittedly I am a noob. Big time.

    Thank you for pointing me to the documentation, but can you tell me why it doesn't work that way?

    Looking at the example :

    <?php
    $xml = '<?xml version="1.0" encoding="UTF-8" ?>
    <rss>
    <channel>
    <item>
    <title><![CDATA[Tom & Jerry]]></title>
    </item>
    </channel>
    </rss>';

    $xml = simplexml_load_string($xml);

    // echo does the casting for you
    echo $xml->channel->item->title;

    // but vardump (or print_r) not!
    var_dump($xml->channel->item->title);

    // so cast the SimpleXML Element to 'string' solve this issue
    var_dump((string) $xml->channel->item->title);
    ?>

    Above will output:

    Tom & Jerry



    My code looks the same, except I am picking an array element because I don't just have one item in the XML document, like this example.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,392
    Rep Power
    594
    You need to use [0] to indicate which element you want to access (in this case the first element, zero based).
    PHP Code:
    echo $xml->channel->item[0]->title
    Obviously you would put this in a loop and rather than use 0 you would use an index variable.

    P.S. Note the use of PHP tags. See the sticky at the top of this forum.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Originally Posted by gw1500se
    You need to use [0] to indicate which element you want to access (in this case the first element, zero based).
    PHP Code:
    echo $xml->channel->item[0]->title
    Obviously you would put this in a loop and rather than use 0 you would use an index variable.

    P.S. Note the use of PHP tags. See the sticky at the top of this forum.
    Is that not what I am doing, here?

    <?php

    $html=$xml->Table[1]->CATDES;

    ?>


    category description, from table number 2 [1]
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,392
    Rep Power
    594
    No. There is only 1 table but you have multiple CATID nodes. That is where the[] belongs. Do a var_dump($xml) so you can see the layout of the whole thing. That should make it easier to see what needs to be indexed.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,548
    Rep Power
    2337
    Try this

    php Code:
    <?php
     
    $xml=simplexml_load_file($URL_VALUE);
    if ($xml === false)
    	echo "Load file failed\n";
    else
    {
    	$html=$xml->Table[1]->CATDES;
    	echo $html;
    }


    Nobody thinks calls ever fail.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    <?php

    $URL_VALUE="URL";
    $xml=simplexml_load_file($URL_VALUE);

    $html=$xml->Table->CATDES[1];


    print_r($html);
    echo $html;
    var_dump($html);

    ?>


    So Changing the code to reference that data, the error about the object goes away, but now returns a null for $html.

    Var dump data on $xml is: (partial)

    object(SimpleXMLElement)#1 (1) { [0]=> string(44236) " 0UNASSIGNED locked
    1ACCESSORIES MISCELLANEOUS Type Model or Style Color Size Material Gauge Mount Battery Config Height Model Fit
    2AIR GUNS Type Action Caliber Capacity Finish Barrel Length Sights Weight Stock Material Length
    3ATV ACCESSORIES Type Model Color Material Size Additional Info Hardware Included Vinyl Type Weight Capacity Hitch Type
    4BLANK ROUNDS Type Caliber Rounds Per Box Hull Material Chamber Shell Type


    All I want is to return the value ACCESSORIES MISCELLANEOUS so I can start poking it into databases.
  16. #9
  17. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,548
    Rep Power
    2337
    I copied your XML to a file named acc.xml (after making it actually valid by closing the NewDataSet tag), and used this exact program, named acc.php:

    php Code:
    <?php
     
    $URL_VALUE = 'acc.xml';
    $xml=simplexml_load_file($URL_VALUE);
    if ($xml === false)
    	echo "Load file failed\n";
    else
    {
    	$html=$xml->Table[1]->CATDES;
    	echo $html . "\n";
    }


    Ran it on the command line and got the following output:
    Code:
    iridium:devel$ php -f acc.php 
    ACCESSORIES MISCELLANEOUS 
    iridium:devel$
    Version:
    Code:
    iridium:devel$ php -v
    PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 28 2012 18:19:13) 
    Copyright (c) 1997-2012 The PHP Group
    Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    So it's working for me.
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Originally Posted by ptr2void
    I copied your XML to a file named acc.xml (after making it actually valid by closing the NewDataSet tag), and used this exact program, named acc.php:

    php Code:
    <?php
     
    $URL_VALUE = 'acc.xml';
    $xml=simplexml_load_file($URL_VALUE);
    if ($xml === false)
    	echo "Load file failed\n";
    else
    {
    	$html=$xml->Table[1]->CATDES;
    	echo $html . "\n";
    }


    Ran it on the command line and got the following output:
    Code:
    iridium:devel$ php -f acc.php 
    ACCESSORIES MISCELLANEOUS 
    iridium:devel$
    Version:
    Code:
    iridium:devel$ php -v
    PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 28 2012 18:19:13) 
    Copyright (c) 1997-2012 The PHP Group
    Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
    So it's working for me.


    Thank you for that. I tested like you, and got the same result.

    My Code

    <?php

    $xml=simplexml_load_file('acc.xml');
    $html=$xml->Table[1]->CATDES;
    echo $html;

    ?>

    The var dump on that is significantly different as well.

    object(SimpleXMLElement)#1 (1) { ["Table"]=> array(3) { [0]=> object(SimpleXMLElement)#2 (12) { ["CATID"]=> string(1) "0" ["CATDES"]=> string(30) "UNASSIGNED " ["ATTR1"]=> string(20) "locked " ["ATTR2"]=> object(SimpleXMLElement)#5 (0) { } ["ATTR3"]=> object(SimpleXMLElement)#6 (0) { } ["ATTR4"]=> object(SimpleXMLElement)#7 (0) { } ["ATTR5"]=> object(SimpleXMLElement)#8 (0) { } ["ATTR6"]=> object(SimpleXMLElement)#9 (0) { } ["ATTR7"]=> object(SimpleXMLElement)#10 (0) { } ["ATTR8"]=> object(SimpleXMLElement)#11 (0) { } ["ATTR9"]=> object(SimpleXMLElement)#12 (0) { } ["ATTR0"]=> object(SimpleXMLElement)#13 (0) { } } [1]=> object(SimpleXMLElement)#3 (12) { ["CATID"]=> string(1) "1" ["CATDES"]=> string(30) "ACCESSORIES MISCELLANEOUS " ["ATTR1"]=> string(20) "Type " ["ATTR2"]=> string(20) "Model or Style " ["ATTR3"]=> string(20) "Color " ["ATTR4"]=> string(20) "Size " ["ATTR5"]=> string(20) "Material " ["ATTR6"]=> string(20) "Gauge " ["ATTR7"]=> string(20) "Mount " ["ATTR8"]=> string(20) "Battery Config " ["ATTR9"]=> string(20) "Height " ["ATTR0"]=> string(20) "Model Fit " } [2]=> object(SimpleXMLElement)#4 (12) { ["CATID"]=> string(1) "2" ["CATDES"]=> string(30) "AIR GUNS " ["ATTR1"]=> string(20) "Type " ["ATTR2"]=> string(20) "Action " ["ATTR3"]=> string(20) "Caliber " ["ATTR4"]=> string(20) "Capacity " ["ATTR5"]=> string(20) "Finish " ["ATTR6"]=> string(20) "Barrel Length " ["ATTR7"]=> string(20) "Sights " ["ATTR8"]=> string(20) "Weight " ["ATTR9"]=> string(20) "Stock Material " ["ATTR0"]=> string(20) "Length " } } }



    Is it my source data having a problem? Is it not formatting correctly? This forum wont let me post a link, otherwise I'd dump the data source.
  20. #11
  21. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,548
    Rep Power
    2337
    If you can't post a URL, just change it to xxxx://whatever
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  22. #12
  23. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    xxxx://www.ssicat.com/smart/updates.asmx/CategoryUpdate?CustomerNumber=15753&UserName=15753&Password=75035&Source=15753"
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Also when that link is pasted into either Firefox for Chrome, I get

    This XML file does not appear to have any style information associated with it. The document tree is shown below.
  26. #14
  27. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,548
    Rep Power
    2337
    That's a disgusting API, because what it's doing is returning an XML document with a single root element, string, containing the XML you want.

    Example:
    Code:
    php -f acc.php 
    <?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://www.ssicat.com/SMART/Updates">&lt;NewDataSet&gt;
      &lt;Table&gt;
        &lt;CATID&gt;0&lt;/CATID&gt;
        &lt;CATDES&gt;UNASSIGNED                    &lt;/CATDES&gt;
        &lt;ATTR1&gt;locked              &lt;/ATTR1&gt;
        &lt;ATTR2 xml:space="preserve"&gt;                    &lt;/ATTR2&gt;
        &lt;ATTR3 xml:space="preserve"&gt;                    &lt;/ATTR3&gt;
        &lt;ATTR4 xml:space="preserve"&gt;                    &lt;/ATTR4&gt;
        &lt;ATTR5 xml:space="preserve"&gt;                    &lt;/ATTR5&gt;
        &lt;ATTR6 xml:space="preserve"&gt;                    &lt;/ATTR6&gt;
        &lt;ATTR7 xml:space="preserve"&gt;                    &lt;/ATTR7&gt;
        &lt;ATTR8 xml:space="preserve"&gt;                    &lt;/ATTR8&gt;
        &lt;ATTR9 xml:space="preserve"&gt;                    &lt;/ATTR9&gt;
        &lt;ATTR0 xml:space="preserve"&gt;                    &lt;/ATTR0&gt;
      &lt;/Table&gt;
    Here's how you need to do it:
    php Code:
    <?php
     
    $uri = 'http://www.ssicat.com/smart/updates.asmx/CategoryUpdate?CustomerNumber=15753&UserName=15753&Password=75035&Source=15753';
    // Get the data
    $data = file_get_contents($uri);
    // Parse the wrapping xml
    $wrappingXml = simplexml_load_string($data);
     
    // Decode the encoded XML entities and parse that
    $actualXml = simplexml_load_string(html_entity_decode($wrappingXml));
     
    // Now you can get your data.
    $html = $actualXml->Table[1]->CATDES;
    echo $html ."\n";
    return;
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    26
    Rep Power
    0
    Originally Posted by ptr2void
    That's a disgusting API, because what it's doing is returning an XML document with a single root element, string, containing the XML you want.

    Example:
    Code:
    php -f acc.php 
    <?xml version="1.0" encoding="utf-8"?>
    <string xmlns="http://www.ssicat.com/SMART/Updates">&lt;NewDataSet&gt;
      &lt;Table&gt;
        &lt;CATID&gt;0&lt;/CATID&gt;
        &lt;CATDES&gt;UNASSIGNED                    &lt;/CATDES&gt;
        &lt;ATTR1&gt;locked              &lt;/ATTR1&gt;
        &lt;ATTR2 xml:space="preserve"&gt;                    &lt;/ATTR2&gt;
        &lt;ATTR3 xml:space="preserve"&gt;                    &lt;/ATTR3&gt;
        &lt;ATTR4 xml:space="preserve"&gt;                    &lt;/ATTR4&gt;
        &lt;ATTR5 xml:space="preserve"&gt;                    &lt;/ATTR5&gt;
        &lt;ATTR6 xml:space="preserve"&gt;                    &lt;/ATTR6&gt;
        &lt;ATTR7 xml:space="preserve"&gt;                    &lt;/ATTR7&gt;
        &lt;ATTR8 xml:space="preserve"&gt;                    &lt;/ATTR8&gt;
        &lt;ATTR9 xml:space="preserve"&gt;                    &lt;/ATTR9&gt;
        &lt;ATTR0 xml:space="preserve"&gt;                    &lt;/ATTR0&gt;
      &lt;/Table&gt;
    Here's how you need to do it:
    php Code:
    <?php
     
    $uri = 'http://www.ssicat.com/smart/updates.asmx/CategoryUpdate?CustomerNumber=15753&UserName=15753&Password=75035&Source=15753';
    // Get the data
    $data = file_get_contents($uri);
    // Parse the wrapping xml
    $wrappingXml = simplexml_load_string($data);
     
    // Decode the encoded XML entities and parse that
    $actualXml = simplexml_load_string(html_entity_decode($wrappingXml));
     
    // Now you can get your data.
    $html = $actualXml->Table[1]->CATDES;
    echo $html ."\n";
    return;

    I will attempt that later, as I am about to go out for a while. Thank you so much. It is refreshing to speak with folks on a forum interested in helping and not flaming. You are a gentleman, sir.
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo