Request example of parsing ORCID XML using php

175 views
Skip to first unread message

Yosef Branse

unread,
Mar 15, 2018, 7:31:06 AM3/15/18
to ORCID API Users
Hello,

Can anyone show me an example of using php to parse the XML returned by an ORCID API call?
Although I have experience reading XML files, the use of namespaces requires
special techniques, which I haven't been able to apply.
The attached file, keyword.xml, containing 'common' and 'keyword' namespaces,
is an example - how do I extract the specific data such as  'created-date', 'content', etc.?
Any assistance would be greatly appreciated.

Thanks,
Yosef Branse
University of Haifa
keyword.xml

jeffre...@ost.orcid.org

unread,
Mar 15, 2018, 1:39:51 PM3/15/18
to ORCID API Users
Hi Yosef,

Attaching a code snippet 

<?php

$xml = simplexml_load_file("./keyword.xml");
$xml->registerXPathNamespace('kw', 'http://www.orcid.org/ns/keyword');
foreach($xml->xpath('//kw:keywords') as $k) {
    $k->registerXPathNamespace('kw', 'http://www.orcid.org/ns/keyword');
    //var_export($k->xpath('//kw:keyword'));
    foreach($k->xpath('//kw:keyword') as $c) {
        $c->registerXPathNamespace('co', 'http://www.orcid.org/ns/common');
        $c->registerXPathNamespace('kw', 'http://www.orcid.org/ns/keyword');
        $created_date_sxe_arr = $c->xpath('//co:created-date');
        print("\ncreated-date: ".$created_date_sxe_arr[0]);
        $keyword_content_sxe_arr = $c->xpath('//kw:content');
        print("\ncontent: ".$keyword_content_sxe_arr[0]);
    }
}
print "\n";

using your xml file.



Cheers.
Reply all
Reply to author
Forward
0 new messages