0

Hi I have been struggling with this puzzle for some time now how to get data out of this embedded namespaces here is the xml feed I have cut it down to make easier to 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 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>

I am trying to access the <crm:reference><crm:name><crm:openedDate> namespace but failing I have looked at similar projects but I think its a namespace within a namespace here is my attempt at parsing the data

<?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);

any pointers to my problem would help

2 Answers 2

1
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";
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

It could be as simple as:

$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>";

If you would like to use a foreach loop to get all children of 'crm' you can do the following. You need atleast 5.2.0 if you want to set children to prefix (true)

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

thank you this has been bothering ever since I answered Ajking11 question on "problems displaying json data retrieved using php curl"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.