【问题标题】:Problems with parsing xml with namespaces使用命名空间解析 xml 的问题
【发布时间】:2017-02-10 05:19:27
【问题描述】:

我需要解析 xml 文件并检索一些信息。但 Simple_XML_string 函数不返回 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 获取信息并且我需要检索

<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>

我正在尝试使用 php(simple_xml_string 函数)进行解析。

有什么想法吗?

【问题讨论】:

    标签: php xml parsing xpath simplexml


    【解决方案1】:

    我已经使用 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]);
      };
    

    【讨论】:

      猜你喜欢
      • 2014-09-16
      • 2012-08-28
      • 2011-04-07
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 2021-05-03
      • 2014-01-10
      相关资源
      最近更新 更多