代码之家  ›  专栏  ›  技术社区  ›  bbutle01

simplexml帮助如何解析这个?

  •  3
  • bbutle01  · 技术社区  · 15 年前

    我正在使用curl向salesforce发出请求,他们会返回一个我需要解析的响应。我想使用simplexml。以下是部分回应:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>
    <loginResponse>
    <result>
    <metadataServerUrl>
    https://na6-api.salesforce.com/services/Soap/m/18.0/
    </metadataServerUrl>
    <passwordExpired>
    false
    </passwordExpired>
    <sandbox>
    false
    </sandbox>
    <serverUrl>
    https://na6-api.salesforce.com/services/Soap/u/18.0/
    </serverUrl>
    <sessionId>
    !AQ4AQLtDIqY.
    </sessionId>
    <userId>
    
    </userId>
    <userInfo>
    <accessibilityMode>
    false
    </accessibilityMode>
    <currencySymbol>
    $
    </currencySymbol>
    <orgDefaultCurrencyIsoCode>
    USD
    </orgDefaultCurrencyIsoCode>
    <orgDisallowHtmlAttachments>
    false
    </orgDisallowHtmlAttachments>
    <orgHasPersonAccounts>
    false
    </orgHasPersonAccounts>
    <organizationId>
    
    </organizationId>
    <organizationMultiCurrency>
    false
    </organizationMultiCurrency>
    <organizationName>
    Ox 
    </organizationName>
    <profileId>
    sdfgsdfg
    </profileId>
    <roleId>
    sdfgsdfg
    </roleId>
    <userDefaultCurrencyIsoCode xsi:nil="true"/>
    <userEmail>
    ###@gmail.com
    </userEmail>
    <userFullName>
    ### ###
    </userFullName>
    <userId>
    asdfasdf
    </userId>
    <userLanguage>
    en_US
    </userLanguage>
    <userLocale>
    en_US
    </userLocale>
    <userName>
    asdfasdf@gmail.com
    </userName>
    <userTimeZone>
    America/Chicago
    </userTimeZone>
    <userType>
    Standard
    </userType>
    <userUiSkin>
    Theme3
    </userUiSkin>
    </userInfo>
    </result>
    </loginResponse>
    </soapenv:Body>
    </soapenv:Envelope>
    

    $results = simplexml_load_string($data);
    var_dump($results);
    

    那会把所有的数据都还给我。。。然后要访问特定部分,需要$results->车身->登录响应->废话->废话。。。

    但它并没有给我这些,也没有给我任何回报,只是一个空的简单xml对象。。。

    所以一个网站让我觉得我可能需要一个XSLT来正确地阅读这个。

    救命啊!

    8 回复  |  直到 15 年前
        1
  •  1
  •   salathe    15 年前

    您可以使用SimpleXML,但它并不像 soapenv ). 研究使用 SimpleXMLElement::children 比如:

    $sxe = new SimpleXMLElement($data);
    $login_response = $sxe->children('soapenv', TRUE)->Body->children('', TRUE)->loginResponse->result;
    // Now that we have the <loginResponse> lets take a look at an item within it
    echo $login_response->userInfo->userEmail;
    

    最后,重要的是,你看过salesforce的 tools & examples ?

        2
  •  2
  •   Dormilich    15 年前
        3
  •  1
  •   Jason    15 年前

    伙计,

    名称空间通常要求您使用子元素进行调用以返回名称空间元素。我建议使用像php-soapclient这样的soap客户机,但因为我以前从未使用过它,所以还有一个可能的选择。

    $results = simplexml_load_string($data);
    $xml = $results->children('http://schemas.xmlsoap.org/soap/envelope/');
    var_dump($xml);
    

    我相信这就是它的工作原理。

        4
  •  0
  •   Josh Andreas Rehm    15 年前

    不管它值多少钱,你可能会发现你使用 PHP SoapClient 对于此任务。O'Reilly有一个 good tutorial

        5
  •  0
  •   Ryan Guest    15 年前

    PHP Toolkit 给Salesforce.com打SOAP电话

        6
  •  0
  •   Amigo Chan    12 年前

    children('soapenv',真) http://schemas.xmlsoap.org/soap/envelope/ ') 工作。

    因此,要读取Salesforce出站消息中的字段值CreatedDate,我需要以下代码:

    $rcXML->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://soap.sforce.com/2005/09/outbound')->notifications->Notification->sObject->children('urn:sobject.enterprise.soap.sforce.com')->CreatedDate
    

    为了帮助您理解它是如何工作的,我用sames代码和xml写了一篇文章,这些代码和xml应该更容易理解。

    http://amigotechnotes.wordpress.com/2013/11/16/parse-xml-with-namespace-by-simplexml-in-php/

        7
  •  0
  •   Wandering Zombie    11 年前

    Parsing soap responses with SimpleXML

    对于希望从UPS Rating API获得RateResponse的人,以下是方法:

    // $client is your SoapClient object
    
    $dom = new DOMDocument;
    
    $dom->loadXML($client->__getLastResponse());
    
    $xml = simplexml_load_string($dom->saveXML(), NULL, NULL, 'http://schemas.xmlsoap.org/soap/envelope/');
    
    $RateResponse = $xml->xpath('/soapenv:Envelope/soapenv:Body')[0]->children('rate', true)->RateResponse;
    
    foreach($RateResponse->RatedShipment as $RatedShipment) {
        print_r((array)$RatedShipment);
    }
    
        8
  •  0
  •   Raju Singh    9 年前

    对于SAOP请求,我们可以通过用blank替换SOAP-ENV:tag,用一段简短的代码轻松地进行解析

    $response = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soapenv:Body>';
    
    $response = html_entity_decode($response);
    $response = str_replace(['soapenv:', 'ns1:', ':ns1', 'SOAP-ENV:'], ['', '', '', ''], $response);
    $objXmlData = simplexml_load_string($response);