Page 1 of 1

simplexml_load_string does not return anything

Posted: Wed Jan 20, 2010 9:05 am
by yacahuma
I dont understand why I am not getting anything out of simplexml_load_string. What am I missing?

Code: Select all

 
$xml = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
        <ns1:createBanResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://ws.interfaces.sessions.APILink.amdocs">
            <createBanReturn href="#id0" />
        </ns1:createBanResponse>
        <multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:OutCreateBanInfo" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="http://io.datainfo.APILink.amdocs">
            <ban xsi:type="xsd:int">688666259</ban>
        </multiRef>
    </soapenv:Body>
</soapenv:Envelope>
XML;
   $root = simplexml_load_string($xml);
   var_dump($root);
 
The output
object(SimpleXMLElement)#1 (0) { }

Re: simplexml_load_string does not return anything

Posted: Wed Jan 20, 2010 9:27 am
by requinix
You're missing namespacing.

When you var_dump $root you get the child nodes in the standard XML namespace. In yours there aren't any.

Code: Select all

var_dump($root->children("http://schemas.xmlsoap.org/soap/envelope/"));
That'll show you the Body.

Anywhere there's an xmlns:foo attribute it defines the foo namespace. Use whatever value (typically a URI) it gives as an argument to children() or attributes().
To get to <ban>'s type you need

Code: Select all

define("XMLNS_SOAPENV", "http://schemas.xmlsoap.org/soap/envelope/");
define("XMLNS_XSI", "http://www.w3.org/2001/XMLSchema-instance");
 
echo $root
    ->children(XMLNS_SOAPENV)->Body // change to soapenv namespace
    ->children("")->multiRef // change to default namespace
    ->ban // still in default namespace
    ->attributes(XMLNS_XSI) // attributes in the xsi namespace
    ->type
;
But the real question here is why you aren't dealing with Soap stuff using Soap stuff.

Re: simplexml_load_string does not return anything

Posted: Wed Jan 20, 2010 4:45 pm
by yacahuma
The thing is that I doing my own web servide test tool. Why? It will be a pain to use a generic tool(too many rules). So I am saving many differents types of calls. Some of those calls, I need to get values back. This is one of them. So I want to be a ble to pick up values, on the fly, not by programing. For example I will have a string in the xml like

call one: create something
call two: <xxx>[GET_LAST_SOMETHING]</xxx>

I dont want to know the called I made before, I just want to get some piece of information and continue.

it is actully working pretty weel so far. For example I have a code table that matches a sql call. so get I use [CODE_XXX] in my request(by the way I am using curl) it goes , get data from the database based on the code and it replaces that into the xml befoire sending it by curl. All I wanted know was being able to read data back to continue some steps.

Re: simplexml_load_string does not return anything

Posted: Thu Jan 21, 2010 1:06 pm
by yacahuma
this works too

Code: Select all

 
$root = simplexml_load_string($xml);
$root->registerXPathNamespace("a", XMLNS_SOAPENV);
$ban = $root->xpath("a:Body/multiRef/ban");
echo "DATA IS " . (string) $ban[0];
 

Re: simplexml_load_string does not return anything

Posted: Thu Jan 21, 2010 1:25 pm
by yacahuma
And the plot thickens..

Why am I getting an array from this code????. I thought I would get a string.

Code: Select all

 
$ban = $root->xpath("a:Body/multiRef/ban/text()");
 
array(1) { [0]=> object(SimpleXMLElement)#2 (1) { [0]=> string(9) "688666259" } } DATA IS Array

Re: simplexml_load_string does not return anything

Posted: Thu Jan 21, 2010 2:04 pm
by yacahuma
never mind my last post xpath always returns an array(Oopss)