0

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

2
Contributors
5
Replies
14
Views
2 Years
Discussion Span
Last Post by t_thakar
0

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 );

?>
0

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

0

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

0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.