当存在双xml标记时从xml soap响应中获取元素值

时间:2015-12-10 06:59:27

标签: php xml soap

我有来自SOAP CALL的XML响应:

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><loginResponse xmlns="http://wws.adomain.com/"><loginResult><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:MainDataTable="replay" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="replay"><xs:complexType><xs:sequence><xs:element name="code" type="xs:string" minOccurs="0" /><xs:element name="description" 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"><DocumentElement xmlns=""><replay diffgr:id="replay1" msdata:rowOrder="0" diffgr:hasChanges="inserted"><code>OK</code><description>
<?xml version="1.0" encoding="UTF-8"?><root><token>e6d354f2-b284569e20-b2def8f3ef4a</token><nmDealer>SAP001 NAME</nmDealer><idRegDealer>8763</idRegDealer><idRegGrupo>-1</idRegGrupo><idRegPuntoVenta>-1</idRegPuntoVenta><idRegUsuario>35350731</idRegUsuario><idRegVendedor>-1</idRegVendedor><idRegZona>-1</idRegZona><dsTpUsuario>CLIENTES</dsTpUsuario><dsPais>PRODUCCION</dsPais><idioma>EN</idioma><idRegIdioma>9</idRegIdioma><isImputar>False</isImputar><moneda>€</moneda><tpUsuario>1</tpUsuario><idRegComisionVariableDealer>-1</idRegComisionVariableDealer><permitirComisionVariableDealer>1</permitirComisionVariableDealer><firstAccess>0</firstAccess><acceptedConditions>False</acceptedConditions><idRegInsured>-1</idRegInsured><idRegAuditor>-1</idRegAuditor></root></description></replay></DocumentElement></diffgr:diffgram></loginResult></loginResponse></soap:Body></soap:Envelope>

http://prntscr.com/9cfdwk

我希望能够在第二个xml标记(<?xml version="1.0" encoding="UTF-8"?>)中获取元素的值(例如,标记)。任何提示帮助都将受到赞赏。

这是我的代码:

$s = new soapclientw($wsdlfile);
if (empty($proxyhost))
{

}
else{
    $s->setHTTPProxy($proxyhost,$proxyport,$proxyusr,$proxypassword);
}
$result = $s->send($msg,'http://wws.domain.com/login',60);

$myXMLData = $s->responseData;

libxml_use_internal_errors(true);

$xml = simplexml_load_string($myXMLData);

if ($xml === false) {
    echo "Failed loading XML: ";
    foreach(libxml_get_errors() as $error) {
        echo "<br>", $error->message;
    }
} 
else{
    echo $xml->token;
}

2 个答案:

答案 0 :(得分:0)

鉴于你的原始,非常可疑的xml,以下是一个黑客但它的工作..

    $strxml='
    <?xml version="1.0" encoding="utf-8"?>
        <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
            <soap:Body>
                <loginResponse xmlns="http://wws.adomain.com/">
                    <loginResult>
                        <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:MainDataTable="replay" msdata:UseCurrentLocale="true">
                                <xs:complexType>
                                    <xs:choice minOccurs="0" maxOccurs="unbounded">
                                        <xs:element name="replay">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element name="code" type="xs:string" minOccurs="0" />
                                                    <xs:element name="description" 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">
                            <DocumentElement xmlns="">
                                <replay diffgr:id="replay1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
                                    <code>OK</code>
                                    <description>
                                        <?xml version="1.0" encoding="UTF-8"?>
                                        <root>
                                            <token>e6d354f2-b284569e20-b2def8f3ef4a</token>
                                            <nmDealer>SAP001 NAME</nmDealer>
                                            <idRegDealer>8763</idRegDealer>
                                            <idRegGrupo>-1</idRegGrupo>
                                            <idRegPuntoVenta>-1</idRegPuntoVenta>
                                            <idRegUsuario>35350731</idRegUsuario>
                                            <idRegVendedor>-1</idRegVendedor>
                                            <idRegZona>-1</idRegZona>
                                            <dsTpUsuario>CLIENTES</dsTpUsuario>
                                            <dsPais>PRODUCCION</dsPais>
                                            <idioma>EN</idioma>
                                            <idRegIdioma>9</idRegIdioma>
                                            <isImputar>False</isImputar>
                                            <moneda>€</moneda>
                                            <tpUsuario>1</tpUsuario>
                                            <idRegComisionVariableDealer>-1</idRegComisionVariableDealer>
                                            <permitirComisionVariableDealer>1</permitirComisionVariableDealer>
                                            <firstAccess>0</firstAccess>
                                            <acceptedConditions>False</acceptedConditions>
                                            <idRegInsured>-1</idRegInsured>
                                            <idRegAuditor>-1</idRegAuditor>
                                        </root>
                                    </description>
                                </replay>
                            </DocumentElement>
                        </diffgr:diffgram>
                    </loginResult>
                </loginResponse>
            </soap:Body>
        </soap:Envelope>';


    $strxml=htmlentities( $strxml );
    $search=array( 
        htmlentities( '<?xml version="1.0" encoding="utf-8"?>' ),
        htmlentities( '<?xml version="1.0" encoding="UTF-8"?>' )
    );
    $strxml=html_entity_decode( str_replace( $search, '', $strxml ) );

    libxml_use_internal_errors( true );
    $dom = new DOMDocument('1.0','utf-8');
    $dom->validateOnParse=false;
    $dom->standalone=true;
    $dom->preserveWhiteSpace=true;
    $dom->strictErrorChecking=false;
    $dom->substituteEntities=false;
    $dom->recover=true;
    $dom->formatOutput=false;
    $dom->loadXML( $strxml );
    libxml_clear_errors();

    $col=$dom->getElementsByTagName('root')->item(0);
    if( $col ){
        foreach( $col->childNodes as $node ) echo $node->tagName.' '.$node->nodeValue.BR;   
    }
    $dom=null;

答案 1 :(得分:0)

谢谢大家,我能够自己解决这个问题。 以下代码对我有用:

$myXMLData = strip_tags($s->responseData);
$temp = explode("OK", $myXMLData);
$myXMLData = $temp[1];
$xml = new SimpleXMLElement(htmlspecialchars_decode($myXMLData));

我剥掉了所有的肥皂标签,用最终的xml数据生活了我,但带有一个前导'OK',我通过explode()删除了它。用空字符串替换'OK'也可以。