0

I have the following string and I want to parse username and password from this string ...

 $xmlstring='<soap-env:envelope xmlns:ns1="http://back.com/"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" 
            xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
            <soap-env:header></soap-env:header>
            <soap-env:body>
               <ns1:createuserresponse>
                  <username>qqq_d481</username>
                  <password>sdfdssdfds</password>
                  <result>
                     <succeeded>true</succeeded>
                     <errorcode>0</errorcode>
                     <errortext></errortext>
                  </result>
               </ns1:createuserresponse>
            </soap-env:body>
           </soap-env:envelope>';

Please suggest.

above string is SOAP response

but if I use:

$xmlstring='<soap-env:envelope>
<soap-env:header></soap-env:header>
<soap-env:body>
    <ns1:createuserresponse>
        <username>qqq_d481</username>
        <password>sdfdssdfds</password>
        <result>
            <succeeded>true</succeeded>
            <errorcode>0</errorcode>
            <errortext></errortext>
        </result>
    </ns1:createuserresponse>
</soap-env:body></soap-env:envelope>';



echo $xmltoparse= $xmlstring;
$xml = simplexml_load_string($xmltoparse);
print_r($xml); 

then it works fine

Cœur
  • 37,241
  • 25
  • 195
  • 267
Anand
  • 21
  • 7
  • Are you sure that your example work? because you are parsing `"my xml string"` in your example – Anas Jul 14 '12 at 10:49

3 Answers3

0

Maybe you could use the PHP5 Soap Client, instead of the simple_xml_parser as suggested here How to parse SOAP XML?

Community
  • 1
  • 1
gherkins
  • 14,603
  • 6
  • 44
  • 70
0

Not sure i fully understand what you are trying to do but maybe something like this?

** untested code but may help

public function parseLoginResponse($loginResponse)
{
    $match = array(
        'username' => 'username',
        'password' => 'password',
        'succeeded' => 'succeeeded',
        'errortext' => 'errortext'
    );

    $this->_match($match, $loginResponse);

    return array(
        'user' => $this->username,
        'pass' => $this->password,
        'success' => $this->succeeded,
        'error' => $this->errortext
    );
}
Vince Lowe
  • 3,521
  • 7
  • 36
  • 63
0

Try using following the asXML function will give you an xml formatted string from

print_r(htmlentities($xmldata->asXML()));

for more information check the following link

Simplexml_load_string($string) returns an empty object but $string contains xml? code below

Community
  • 1
  • 1
Sumit Neema
  • 462
  • 3
  • 18