0

i'm trying to parse data in xml with simple_load_string function in php and it's return an empty data in the $parsed (data parsed) .

the data to parse is like :

<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
    <cas:authenticationSuccess>
        <cas:user>yassine458</cas:user>
    </cas:authenticationSuccess>
</cas:serviceResponse>

is that possible to parse data with this format or there is another method to do it .

this is my code :

$xml = simplexml_load_string($result);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
$parsed= simplexml_load_string($result);

i'm trying to convert it to json format

6
  • Show us the code as well as the data Commented Jun 8, 2017 at 12:06
  • you can take a look Commented Jun 8, 2017 at 12:08
  • is there a solution or not ? Commented Jun 8, 2017 at 12:49
  • 1
    That is not valid XML so it is difficult to say any more than that as I cannot test it Commented Jun 8, 2017 at 12:51
  • i can get that there is not solution to parse it ? Commented Jun 8, 2017 at 12:59

3 Answers 3

1

When i was working with xml i am facing the same issue but i found this solution and it worked for me

Documentation :- https://github.com/rentpost/xml2array

OR

https://packagist.org/packages/verdant/xml2array

Use

    $xml = '<cas:serviceResponse xmlns:cas="http://www.yale.edu/tp/cas">
        <cas:authenticationSuccess>
            <cas:user>yassine458</cas:user>
        </cas:authenticationSuccess>
    </cas:serviceResponse>';

    $xml2array = XML2Array::createArray($xml);
    echo "<pre>";
    print_r($xml2array);
    echo "<pre>";

Accessing "user"

    echo $xml2array['cas:serviceResponse']['cas:authenticationSuccess']['cas:user'];

Result : -

    Array
    (
        [cas:serviceResponse] => Array
            (
                [cas:authenticationSuccess] => Array
                    (
                        [cas:user] => yassine458
                    )

            )

    )

if you don't want cas: just replace it with "" in xml;

Tested sendbox

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

5 Comments

how could you get the variable cas:user ?
as you can see i have shown how to access the property it just an array use as array
XML2Array is not found when i try to make your code
you have to copy this [github.com/rentpost/xml2array/blob/master/Verdant/… code and make a file with name of XML2Array.php then include this file and it will work
Answer Updated have a look or just sendbox [sandbox.onlinephpfunctions.com/code/…
0
$xml_string = file_get_contents('/path');
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

Then you can use $json or $array as you want.

Comments

0

i find this solution :

$todelete="xmlns:cas=\"http://www.yale.edu/tp/cas\"";

        $result="<cas:serviceResponse xmlns:cas=\"http://www.yale.edu/tp/cas\">
                    <cas:authenticationSuccess>
                     <cas:user>pdurant2</cas:user>
                      </cas:authenticationSuccess>
                        </cas:serviceResponse>";
        if (strpos($result, $todelete) !== false) {
            $result= str_replace($todelete,null,$result);
        }
        if (strpos($result, "cas:") !== false) {
            $result= str_replace("cas:",null,$result);

        }
        if (strpos($result, "/cas:") !== false) {
            $result= str_replace("/cas:",null,$result);

        }
        $parsed=simplexml_load_string($result);

and it's work for me .

Comments

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.