1

I got xml responce by soap api like

$data = <users><user id="1028240" enabled="True" mustChangePassword="False">
<username><![CDATA[Priye1]]></username>
<firstName><![CDATA[Priye]]></firstName>
<middleName></middleName>
<lastName><![CDATA[Priye]]></lastName>
<email></email>
<company></company>
</users>

When i convert $data into simple xml object by

$array = simplexml_load_string($data);

i got object but object does not contain any value.

After converting object as follow

[users] => Array ( [user] => Array ( [0] => Array ( [@attributes] => Array ( [id] => 1028240 [enabled] => True [mustChangePassword] => False )

                            [username] => Array
                                (
                                )

                            [firstName] => Array
                                (
                                )

                            [middleName] => Array
                                (
                                )

                            [lastName] => Array
                                (
                                )

                            [email] => Array
                                (
                                )

                            [company] => Array
                                (
                                )

                            [address] => Array
                                (
                                )

                            [city] => Array
                                (
                                )

                            [province] => Array
                                (
                                )
}

}

5
  • Could you dump the object and add that to the question? Commented Jun 26, 2015 at 17:26
  • Thanks, but I am confused: 1. the first and the second version of the xml response you posted differ. 2. the output you posted is not empty. So what is the question here? Commented Jun 26, 2015 at 17:37
  • The formatting of this question could be improved. Commented Jun 26, 2015 at 17:59
  • @arkascha you can see in question in xml response username and firstname is not empty but after converting in object username and first name is empty. Commented Jun 26, 2015 at 18:06
  • Hm, so that us your actual issue... Why don't you say so in the question? I updated my question below to also output that CDATA stuff. Commented Jun 26, 2015 at 18:35

1 Answer 1

2

It looks like your xml input is invalid. In such case, if you do not any error checking and do not look at the log files where errors are shown, then you might have difficulties to see that...

Here is an example with fixed content:

<?php

$data  = <<<EOT
<users>
  <user id="1028240" enabled="True" mustChangePassword="False">
    <username><![CDATA[Priye1]]></username>
    <firstName><![CDATA[Priye]]></firstName>
    <middleName></middleName>
    <lastName><![CDATA[Priye]]></lastName>
    <email></email>
    <company></company>
  </user>
</users>
EOT;

print_r(simplexml_load_string($data, null, LIBXML_NOCDATA));

Note the added </user> towards the end which closes the user tag opened in the second line. Without that fix you get this warning on CLI or inside your log error log file: PHP Warning: simplexml_load_string(): Entity: line 9: parser error : expected '>'.

Also you have to tell the xml parser that is should access and interpret the contained CDATA content, if you want to. That is due to the fact that such data is not really considered to be part of the xml document itself. You can do that by using the flag LIBXML_NOCDATA as shown in the final print_r() call.

The output of that script is:

SimpleXMLElement Object
(
    [user] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [id] => 1028240
                [enabled] => True
                [mustChangePassword] => False
            )

        [username] => Priye1
        [firstName] => Priye
        [middleName] => SimpleXMLElement Object
            (
            )

        [lastName] => Priye
        [email] => SimpleXMLElement Object
            (
            )

        [company] => SimpleXMLElement Object
            (
            )

    )

)
Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help :-) Maybe you want to upvote or accept my answer :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.