XML feeds and namespaces with PHP, SimpleXML and Xpath -
hi have been struggling puzzle time how data out of embedded namespaces here xml feed have cut down make easier read
<?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 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>
i trying access <crm:reference><crm:name><crm:openeddate>
namespace failing have looked @ similar projects think namespace within namespace here attempt @ parsing data
<?php $xml = simplexml_load_file("sage.xml"); $xml->registerxpathnamespace('sdata', 'http://schemas.sage.com/sdata/sync/2008/1'); foreach($xml->xpath("//sdata:payload") $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 $c) { // echo $c->reference . " | " . $c->name . "/" . $c->accountopeneddate . "<br />\n"; } } var_dump($content); var_dump($c);
any pointers problem help
foreach($xml->xpath("//sdata:payload") $entry) { // xpath here must payload tradingaccount $content = $entry->xpath("./crm:tradingaccount"); foreach($content $c) { // make set of children prefix crm $nodes = $c->children('crm', true); echo $nodes->reference . " | " . $nodes->name . " / " . $nodes->openeddate . "<br />\n"; } }
Comments
Post a Comment