我有以下字符串,我想从该字符串中解析用户名和密码...
$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>';
请提出建议。
字符串以上是SOAP响应
但是如果我使用:
$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);
然后工作正常
也许您可以使用PHP5 Soap Client,而不是simple_xml_parser如此处建议How to parse SOAP XML?
不确定我是否完全理解您要做什么,但是也许是这样?
**未经测试的代码,但可能会有所帮助
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
);
}
尝试使用以下asXML函数将为您提供一个来自xml格式的字符串
print_r(htmlentities($ xmldata-> asXML()));
有关更多信息,请检查以下链接
Simplexml_load_string($string) returns an empty object but $string contains xml? code below