ssD
ssD

Reputation: 359

php xml parser simplexml_load_string

simplexml_load_string() doesnot seem to work form for the following xml

 "<?xml version="1.0" encoding="UTF-8"?>
<Chat_vailable queue="IBIN" locale="gn">Hide</Chat_vailable>"

$result = simplexml_load_string($response->data)

[@attributes]
  queue -> IBIN
  locale->gn

the above xml is part of a response so when get the result it only contains attributes

$result is an object of SimpleXMLElement and only has an array of attributes

It doesn't have anything related to "Chat_valiable" or HIDE.

Could some one help please

Upvotes: 0

Views: 314

Answers (3)

IMSoP
IMSoP

Reputation: 97783

I'm not sure what you're using to inspect the object there, I'm guessing print_r, but whatever it is, don't rely on it. :)

SimpleXML does not create a real PHP object with properties for everything in the XML document, it provides an object-like API linked to the internal parsed representation.

So in your case:

  • Chat_vailable is the root node, so is represented by $result itself (SimpleXML has no separate object for the "document", so there is nothing "above" the root element)
  • Hide is the text content of that node, so can be accessed with a string cast: (string)$result (or just echo $result, since that casts to string automatically)
  • the attributes queue and locale can be accessed using array notation (casting to string is a good habit, to avoid passing around objects which may confuse later functions): (string)$result['queue'], (string)$result['locale']

If you want to inspect the full content available through a SimpleXML object, have a look at these dedicated SimpleXML debug functions.

Upvotes: 1

michi
michi

Reputation: 6625

$xml = simplexml_load_string($x); // assume XML in $x
echo "queue: $xml[queue], locaele: $xml[locale], value: $xml";

will output:

queue: IBIN, locaele: gn, value: Hide

see it working: https://eval.in/39965

Upvotes: 0

DevZer0
DevZer0

Reputation: 13535

performing var_dump on $result yield this. So it does get both attributes and content.

object(SimpleXMLElement)#1 (2) {
  ["@attributes"]=>
  array(2) {
    ["queue"]=>
    string(4) "IBIN"
    ["locale"]=>
    string(2) "gn"
  }
  [0]=>
  string(4) "Hide"
}

code that produce the output above as follows

$xml = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<Chat_vailable queue="IBIN" locale="gn">Hide</Chat_vailable>
EOF;

$result = simplexml_load_string($xml);

var_dump($result);

Upvotes: 0

Related Questions