0

I'm having trouble accessing the child nodes of my SimpleXML content. I've tried a few variations like $xmlData->id or $xmlData['id'].

I'm somewhat new to this area, so any help would be greatly appreciated.

Code sample:

    $xmlData = simplexml_load_string($string);
    $person = $xmlData->documentElement;
    $id = $xmlData->id;

Var dump of $xmlData from Simple XML:

object(SimpleXMLElement)#23 (10) { 
     ["id"]=> string(10) "yTZn1JIaaa" 
     ["first-name"]=> string(6) "First Name" 
     ["last-name"]=> string(6) "Last Name"...
hakre
  • 193,403
  • 52
  • 435
  • 836
jsuissa
  • 1,754
  • 6
  • 30
  • 64
  • 1
    What is the error message? And which child nodes are you asking about? – hakre Jan 30 '12 at 17:14
  • Hm, seems `->id` should work... What is your input XML? – Wrikken Jan 30 '12 at 17:15
  • possible duplicate of [display data from XML using php simplexml](http://stackoverflow.com/questions/5833788/display-data-from-xml-using-php-simplexml) – Gordon Jan 31 '12 at 09:21
  • possible duplicate of [SimpleXML Reading node with a hyphenated name](http://stackoverflow.com/questions/3626901/simplexml-reading-node-with-a-hyphenated-name) – Gordon Jan 31 '12 at 09:22

1 Answers1

1

I tried to rebuild your snippet:

<?php
$string = "<person><id>1</id><firstname>Foo</firstname><lastname>Bar</lastname></person>";
$xmlData = simplexml_load_string($string);
echo $xmlData->id . " - " . $xmlData->firstname . " - " . $xmlData->lastname;
?>

This worked fine for me - the Output was:

1 - Foo - Bar

You can see, that my firstname and lastname tagnames are different to yours - the dash sometimes causes troubles. If you need the dash, do it this way instead:

echo $xmlData->id . " - " . $xmlData->{'first-name'} . " - " . $xmlData->{'last-name'};

Anyways, the id worked fine...so maybe you have another error that prevents your scripts from running properly? Maybe it would be helpful if you could post your XML-String.

Cheers, Max

P.S.: Why are you executing this line?

$person = $xmlData->documentElement;
Max
  • 1,000
  • 1
  • 11
  • 25
  • Thank you very much! Yes the dash was problematic. The {'a'} makes method sense. Everything works now.I originally didn't have the $person = line in the code, but after looking at some more examples tried it. Of course, didn't help. – jsuissa Jan 30 '12 at 18:06
  • @jsuissa technically, this answer is the same as the code you claim doesnt work. can you please point out what the exact problem was that made it now work now all of sudden? – Gordon Jan 30 '12 at 18:26
  • Sure -- it seems that without the surrounding brackets just having $xmlData->first-name failed, but with {'first-name'} like that it worked. I'm not sure why myself. Wondering if this could be related to using Codeigniter? – jsuissa Jan 30 '12 at 23:42
  • @jsuissa no, thats because PHP will interpret $xmlData->first-name as $xmlData->first - name, e.g. it will try to subtract. Hyphens are not valid variables name and if you encounter them in XML you have to use the curly brace syntax. Your question did ask for the id element though and not for the hyphenated elements. Please try to be more specific next time. – Gordon Jan 31 '12 at 09:16