I have a soap call which returns an object which has a xml string.

an example is shown below:

<?xml version="1.0" encoding="utf-16"?>
<LoginResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <StatusCode>1</StatusCode>
  <StatusMessage>Login Successful</StatusMessage>
  <AuthKey>b2365767-ae42-1a44a6436575</AuthKey>
</LoginResponse>

I would like it to convert it into an associative array which will be similar to:

array(
    "LoginResponse"=>array(
        "StatusCode"=>1,
        "StatusMessage"=>"Login Successful"
        "AuthKey"=>"b2365767-ae42-1a44a6436575"
    )
);

Hope this makes sense

I have tried simplexml_load_string()
xml2array()

nothing works

Member Avatar for diafol

Not sure what's not working.

<?php
//LOAD FILE TO STRING
$file = file_get_contents('untitled2.xml');

//LOAD STRING TO OBJECT
$f = new SimpleXMLElement($file);

//INDIVIDUAL ELEMENTS
$output = "Status Code: " . $f->StatusCode . "\n";
$output .= "Status Message: " . $f->StatusMessage . "\n";
$output .= "Auth Key: " . $f->AuthKey;
echo nl2br($output);

//FOR ASSOC ARRAY
$json = json_decode(json_encode($f),true);
print_r( $json );

?>
Member Avatar for diafol

Update. I'm assuming that LoginResponse is the "container" and that there's only one and that there's only one statuscode node etc.

Hi Diafol

Thank you for a quick response...

The xml I receive is in an object..
I extract that into a variable

but I have a variable with the xml string but not a file

Update: I have used $f = simplexml_load_string($xml); instead of the code you have provided but still does not work

//LOAD FILE TO STRING
$file = file_get_contents('untitled2.xml');
//LOAD STRING TO OBJECT
$f = new SimpleXMLElement($file);

it says $f is false

Update: I think I solved it...

What I noticed was..

$obj = simplexml_load_string($xml);

the code above was throwing error and it says "Document labelled UTF-16 but has UTF-8 content"

so I converted replaced utf-16 with utf-8 and it worked

Thanks Diafol for all your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.