2017-02-10 1 views
0

XML 파일을 구문 분석하고 정보를 검색해야합니다. 그러나 Simple_XML_string 함수가 xml의 모든 내용을 반환하지 않습니다.네임 스페이스로 xml을 파싱 할 때의 문제

여기에 다음 xml이 있습니다.

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xml:base="URL"> 
    <id> 
    URL(UserId='',UserType='') 
    </id> 
    <title type="text">RecommendationScenarios(UserId='',UserType='')</title> 
    <updated>2017-02-10T05:08:15Z</updated> 
    <category term="PROD_RECO_RUNTIME_SRV.RecommendationScenario" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/> 
    <link href="RecommendationScenarios(UserId='',UserType='')" rel="self" title="RecommendationScenario"/> 
    <link href="RecommendationScenarios(UserId='',UserType='')/Scenarios" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Scenarios" type="application/atom+xml;type=feed" title="Scenarios"> 
    <m:inline/> 
    </link> 
    <link href="RecommendationScenarios(UserId='',UserType='')/ContextParams" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ContextParams" type="application/atom+xml;type=feed" title="ContextParams"> 
    <m:inline/> 
    </link> 
    <link href="RecommendationScenarios(UserId='',UserType='')/ResultObjects" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/ResultObjects" type="application/atom+xml;type=feed" title="ResultObjects"> 
    <m:inline> 
    <feed xml:base="URL"> 
    <id> 
    URL(UserId='',UserType='')/ResultObjects 
    </id> 
    <title type="text">ResultObject</title> 
    <updated>2017-02-10T05:08:15Z</updated> 
    <author> 
    <name/> 
    </author> 
    <link href="RecommendationScenarios(UserId='',UserType='')/ResultObjects" rel="self" title="ResultObject"/> 
    <entry> 
    <id> 
    URL(ScenarioId='SAP_TOP_SELLER_HOME_PAGE',ResultObjectType='SAP_HYBRIS_PRODUCT',ResultObjectId='37511') 
    </id> 
    <title type="text"> 
    ResultObject(ScenarioId='SAP_TOP_SELLER_HOME_PAGE',ResultObjectType='SAP_HYBRIS_PRODUCT',ResultObjectId='37511') 
    </title> 
    <updated>2017-02-10T05:08:15Z</updated> 
    <category term="PROD_RECO_RUNTIME_SRV.ResultObject" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/> 
    <link href="ResultObject(ScenarioId='SAP_TOP_SELLER_HOME_PAGE',ResultObjectType='SAP_HYBRIS_PRODUCT',ResultObjectId='37511')" rel="self" title="ResultObject"/> 
    <content type="application/xml"> 
    <m:properties> 
    <d:ScenarioId>SAP_TOP_SELLER_HOME_PAGE</d:ScenarioId> 
    <d:ResultObjectType>SAP_HYBRIS_PRODUCT</d:ResultObjectType> 
    <d:ResultObjectId>37511</d:ResultObjectId> 
    <d:ResultObjectScore>1.00000</d:ResultObjectScore> 
    </m:properties> 
    </content> 
    </entry> 

    </feed> 
    </m:inline> 
    </link> 
    <content type="application/xml"> 
    <m:properties> 
    <d:UserId/> 
    <d:UserType/> 
    </m:properties> 
    </content> 
    </entry> 

나는 content type=application에서 정보를 얻을 필요가 그리고 내가 PHP (simple_xml_string 기능)를 사용하여 분석하려고 해요

<m:properties> 
<d:ScenarioId>SAP_TOP_SELLER_HOME_PAGE</d:ScenarioId> 
<d:ResultObjectType>SAP_HYBRIS_PRODUCT</d:ResultObjectType> 
<d:ResultObjectId>4934</d:ResultObjectId> 
<d:ResultObjectScore>0.49999</d:ResultObjectScore> 
</m:properties> 

를 검색 할 필요가있다.

아이디어가 있으십니까?

답변

0

xpath와 namespaceRegister을 사용하여 문제를 해결했습니다.

$xml = new SimpleXMLElement($content); 
    $xml->registerXPathNamespace('m', 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'); 

    $products = []; 
    foreach($xml->xpath('//m:properties') as $key=>$event) { 
     $products[] = strval($event->xpath('d:ResultObjectId')[0]); 
    }; 
관련 문제