【问题标题】:XML feeds and namespaces with PHP, SimpleXML and Xpath使用 PHP、SimpleXML 和 Xpath 的 XML 提要和命名空间
【发布时间】:2015-10-13 17:45:17
【问题描述】:

嗨,我一直在努力解决这个难题一段时间了,如何从这个嵌入式命名空间中获取数据这里是 xml 提要,我已将其删减以使其更易于阅读

 <?xml version="1.0" encoding="utf-8"?>
<feed xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cf="http://www.microsoft.com/schemas/rss/core/2005" xmlns="http://www.w3.org/2005/Atom" xmlns:sc="http://schemas.sage.com/sc/2009" xmlns:crm="http://schemas.sage.com/crmErp/2008" xmlns:sdatasync="http://schemas.sage.com/sdata/sync/2008/1" xmlns:sdata="http://schemas.sage.com/sdata/2008/1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:sme="http://schemas.sage.com/sdata/sme/2007" xmlns:http="http://schemas.sage.com/sdata/http/2008/1">
  <author />
  <category term="tradingAccount" />
  <generator />
  <subtitle>Provides a feed containing tradingAccount details</subtitle>
  <title>Sage Accounts 50 | tradingAccount - Practice Accounts                                                    </title>
  <entry>
    <author />
    <content type="html"><![CDATA[<html>

</html>]]></content>
    <id>http://computer_1:5493/sdata/accounts50/GCRM/{FF476636-D4AF-4191-BDE4-891EDA349A68}/tradingAccountCustomer(58b10585-63d4-4bb8-adb3-7096d9b055d9)?format=atomentry</id>
    <link href="http://computer_1:5493/sdata/accounts50/GCRM/-/tradingAccountCustomer" rel="via" type="application/atom+xml" />
     <published>2015-03-13T21:28:59.000+00:00</published>
    <updated>2015-07-01T21:33:13.000+01:00</updated>
    <http:httpStatus>200</http:httpStatus>
    <sdata:payload>
      <crm:tradingAccount sdata:url="http://computer_1:5493/sdata/accounts50/GCRM/{FF476636-D4AF-4191-BDE4-891EDA349A68}/tradingAccountCustomer(58b10585-63d4-4bb8-adb3-7096d9b055d9)?format=atomentry" sdata:uuid="58b10585-63d4-4bb8-adb3-7096d9b055d9">
        <crm:active>true</crm:active>
        <crm:customerSupplierFlag>Customer</crm:customerSupplierFlag>
        <crm:companyPersonFlag>Company</crm:companyPersonFlag>
        <crm:invoiceTradingAccount xsi:nil="true" />
        <crm:openedDate>2013-04-22</crm:openedDate>
        <crm:reference>AAA</crm:reference>
        <crm:name>BBB</crm:name>
      </crm:tradingAccount>
    </sdata:payload>
  </entry>
  <opensearch:totalResults>118</opensearch:totalResults>
  <opensearch:startIndex>1</opensearch:startIndex>
  <opensearch:itemsPerPage>118</opensearch:itemsPerPage>
</feed>

我正在尝试访问 &lt;crm:reference&gt;&lt;crm:name&gt;&lt;crm:openedDate&gt; 命名空间,但失败了,我查看了类似的项目,但我认为它是命名空间中的命名空间 这是我解析数据的尝试

<?php

$xml = simplexml_load_file("sage.xml");
$xml->registerXPathNamespace('sdata', 'http://schemas.sage.com/sdata/sync/2008/1');
foreach($xml->xpath("//sdata:payload") as $entry) {
    $entry->registerXPathNamespace('sdata', 'http://schemas.sage.com/sdata/sync/2008/1');
    $entry->registerXPathNamespace('crm', 'http://schemas.sage.com/crmErp/2008');
    $content = $entry->xpath("/sdata:payload/crm:tradingAccount/crm:active");
    //$article = feed->xpath("/sdata:payload/crm:tradingAccount/crm:active");
    foreach($content as $c) { 
       // echo $c->reference . " | " . $c->name . "/" . $c->accountOpenedDate . "<br />\n";
    }
}
var_dump($content);
var_dump($c);

任何指向我的问题的指针都会有所帮助

【问题讨论】:

    标签: php xml xpath namespaces


    【解决方案1】:

    可以这么简单:

    $xml = simplexml_load_file("sage.xml");
    
    $reference = $xml->xpath("//crm:reference");
    $name = $xml->xpath("//crm:name");
    $openedDate = $xml->xpath("//crm:openedDate");
    
    echo "reference: ". $reference[0] . "<br>";
    echo "name: ". $name[0] . "<br>";
    echo "openedDate: ". $openedDate[0] . "<br>";
    

    如果您想使用 foreach 循环来获取“crm”的所有子项,您可以执行以下操作。如果要将子项设置为前缀(true),则至少需要 5.2.0

    $xml = simplexml_load_file("sage.xml");
    
    foreach($xml->xpath("//crm:tradingAccount") as $entry) {    
           $child = $entry->children('crm', true);         
    }
    
    echo "reference: ". $child->reference . "<br>";
    echo "name: ". $child->name . "<br>";
    echo "openedDate: ". $child->openedDate . "<br>";
    

    【讨论】:

    • 谢谢,自从我回答 Ajking11 关于“显示使用 php curl 检索的 json 数据的问题”的问题后,这一直很困扰
    【解决方案2】:
    foreach($xml->xpath("//sdata:payload") as $entry) {
        // xpath here must be from payload to tradingAccount
        $content = $entry->xpath("./crm:tradingAccount");
        foreach($content as $c) {
           // Make set of children with prefix crm
           $nodes = $c->children('crm', true); 
           echo $nodes->reference . " | " . $nodes->name . " / " . $nodes->openedDate . "<br />\n";
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-08-07
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-07-05
      • 2011-07-24
      • 2014-04-19
      相关资源
      最近更新 更多