首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP、SimpleXML和Xpath的XML提要和命名空间

使用PHP、SimpleXML和Xpath的XML提要和命名空间
EN

Stack Overflow用户
提问于 2015-07-23 12:13:58
回答 2查看 121关注 0票数 0

嗨,我已经为这个难题挣扎了一段时间了,现在如何从这个嵌入的命名空间中获取数据--这里是xml提要--我已经削减了它,以便更容易阅读。

代码语言:javascript
复制
 <?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>

我试图访问<crm:reference><crm:name><crm:openedDate>名称空间,但失败了,我看过类似的项目,但我认为它是名称空间中的名称空间,这里是我解析数据的尝试。

代码语言:javascript
复制
<?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);

任何关于我的问题的指示都会有帮助

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-07-23 13:21:43

代码语言:javascript
复制
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";
    }
}
票数 1
EN

Stack Overflow用户

发布于 2015-07-23 13:22:50

它可以很简单,如:

代码语言:javascript
复制
$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。

代码语言:javascript
复制
$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>";
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31587041

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档