如何在没有soap客户端php的情况下解析这个soap xml响应

时间:2015-06-05 10:59:48

标签: php xml soap

我必须解析此xml并检索UsernamePassword值。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <ProcessResponse
            xmlns="http://api.securecrmsite.com/">
            <ProcessResult>
                <RequestID>5666290</RequestID>
                <PrimaryKeyID>0</PrimaryKeyID>
                <Success>true</Success>
                <Message>Process completed. Found 1 records.</Message>
                <Records>1</Records>
                <ProcessData>
                    <xs:schema id="NewDataSet"
                        xmlns=""
                        xmlns:xs="http://www.w3.org/2001/XMLSchema"
                        xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
                        <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                            <xs:complexType>
                                <xs:choice minOccurs="0" maxOccurs="unbounded">
                                    <xs:element name="Results">
                                        <xs:complexType>
                                            <xs:sequence>
                                                <xs:element name="CustomerID" type="xs:int" minOccurs="0" />
                                                <xs:element name="Username" type="xs:string" minOccurs="0" />
                                                <xs:element name="Password" type="xs:string" minOccurs="0" />
                                            </xs:sequence>
                                        </xs:complexType>
                                    </xs:element>
                                </xs:choice>
                            </xs:complexType>
                        </xs:element>
                    </xs:schema>
                    <diffgr:diffgram
                        xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
                        xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
                        <NewDataSet
                            xmlns="">
                            <Results diffgr:id="Results1" msdata:rowOrder="0">
                                <CustomerID>2465266</CustomerID>
                                <Username>tamimanother1</Username>
                                <Password>4565AA</Password>
                            </Results>
                        </NewDataSet>
                    </diffgr:diffgram>
                </ProcessData>
                <XmlRequest>&amp;lt;soap:Envelope
                    xmlns:xsi=&amp;quot;http://www.w3.org/2001/XMLSchema-instance&amp;quot;
                    xmlns:xsd=&amp;quot;http://www.w3.org/2001/XMLSchema&amp;quot;
                    xmlns:soap=&amp;quot;http://schemas.xmlsoap.org/soap/envelope/&amp;quot;&amp;gt;&amp;lt;soap:Body&amp;gt;&amp;lt;Process
                    xmlns=&amp;quot;http://api.securecrmsite.com/&amp;quot;&amp;gt;&amp;lt;Credentials&amp;gt;&amp;lt;CompanyKey&amp;gt;3785&amp;lt;/CompanyKey&amp;gt;&amp;lt;APIKey&amp;gt;7ca9d84b-e7c9-4d19-aae1-218d109eab6f&amp;lt;/APIKey&amp;gt;&amp;lt;/Credentials&amp;gt;&amp;lt;ProcessName&amp;gt;CustomerInsert&amp;lt;/ProcessName&amp;gt;&amp;lt;Parameters&amp;gt;&amp;lt;ApiParams&amp;gt;&amp;lt;Name&amp;gt;FirstName&amp;lt;/Name&amp;gt;&amp;lt;Value&amp;gt;tamim&amp;lt;/Value&amp;gt;&amp;lt;/ApiParams&amp;gt;&amp;lt;ApiParams&amp;gt;&amp;lt;Name&amp;gt;LastName&amp;lt;/Name&amp;gt;&amp;lt;Value&amp;gt;another&amp;lt;/Value&amp;gt;&amp;lt;/ApiParams&amp;gt;&amp;lt;ApiParams&amp;gt;&amp;lt;Name&amp;gt;Email&amp;lt;/Name&amp;gt;&amp;lt;Value&amp;gt;test@test.com&amp;lt;/Value&amp;gt;&amp;lt;/ApiParams&amp;gt;&amp;lt;/Parameters&amp;gt;&amp;lt;/Process&amp;gt;&amp;lt;/soap:Body&amp;gt;&amp;lt;/soap:Envelope&amp;gt;
                </XmlRequest>
            </ProcessResult>
        </ProcessResponse>
    </soap:Body>
</soap:Envelope>

我试过这个 - &gt;

```
$xml = str_ireplace(['SOAP-ENV:', 'SOAP:'], '', $xml);
$data = simplexml_load_string($xml);
print_r($data->Body->ProcessResponse->NewDataSet);
```

1 个答案:

答案 0 :(得分:0)

如果你想要xpath,那就试试这个

concat(//Results/Username,"|",//Results/Password) 

结果String='tamimanother1|4565AA'