Search code examples
phpxmlsimplexmlpubmed

Trouble parsing the pubmed central API XML


I'm trying to parse the xml output of the PMC API, yet for some reason I get only errors or empty results.. (had no trouble parsing the Pubmed API..)

This is how the XML tools like :

<pmc-articleset>
  <article xmlns:mml="http://www.w3.org/1998/Math/MathML" article-type="research-article">
    <!--...-->
    <front>
      <journal-meta>
        <journal-id journal-id-type="nlm-ta">Cell Death Differ</journal-id>
        <journal-id journal-id-type="iso-abbrev">Cell Death Differ</journal-id>
        <journal-title-group>
          <journal-title>Cell Death and Differentiation</journal-title>
        </journal-title-group>
        <issn pub-type="ppub">1350-9047</issn>
        <issn pub-type="epub">1476-5403</issn>
        <publisher>...</publisher>
      </journal-meta>
      <article-meta>...</article-meta>
    </front>
  </article>
</pmc-articleset>

And this is my code:

$xml = simplexml_load_file($query);
$journal_name = (string)$xml->{'pmc-articleset'}->article->front->{'journal-meta'}->{'journal-title-group'}->{'journal-title'};
echo "Title: ".$journal_name;

Any advice would be appreciated!


Solution

  • You are already in the article-set element when you start and $article is an element, not a variable, so the $ should be removed.

    echo (string)$xml->article->front->{'journal-meta'}->{'journal-title-group'}->{'journal-title'};
    

    https://3v4l.org/GoTu8