Milad
Milad

Reputation: 5510

Javascript get all tag values with the same tag name

I have the javascript below that fetches an XML feed:

fetch("https://export.arxiv.org/api/query?id_list=1804.10436")
    .then(response => response.text())
    .then(str => new window.DOMParser().parseFromString(str, "text/xml"))

The XML looks like below:

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <link href="http://arxiv.org/api/query?search_query%3D%26id_list%3D1804.10436%26start%3D0%26max_results%3D10" rel="self" type="application/atom+xml"/>
  <title type="html">ArXiv Query: search_query=&amp;id_list=1804.10436&amp;start=0&amp;max_results=10</title>
  <id>http://arxiv.org/api/nUEsN1vTKh1gSfUw4HiR2ZTFdzs</id>
  <updated>2021-04-15T00:00:00-04:00</updated>
  <opensearch:totalResults xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">1</opensearch:totalResults>
  <opensearch:startIndex xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">0</opensearch:startIndex>
  <opensearch:itemsPerPage xmlns:opensearch="http://a9.com/-/spec/opensearch/1.1/">10</opensearch:itemsPerPage>
  <entry>
    <id>http://arxiv.org/abs/1804.10436v1</id>
    <updated>2018-04-27T10:57:45Z</updated>
    <published>2018-04-27T10:57:45Z</published>
    <title>Characterizing the highly cited articles: a large-scale bibliometric
  analysis of the top 1% most cited research</title>   
    <author>
      <name>Pablo Dorta-González</name>
    </author>
    <author>
      <name>Yolanda Santana-Jiménez</name>
    </author>
    <arxiv:comment xmlns:arxiv="http://arxiv.org/schemas/atom">23 pages, 6 tables, 2 figures</arxiv:comment>
    <link href="http://arxiv.org/abs/1804.10436v1" rel="alternate" type="text/html"/>
    <link title="pdf" href="http://arxiv.org/pdf/1804.10436v1" rel="related" type="application/pdf"/>
    <arxiv:primary_category xmlns:arxiv="http://arxiv.org/schemas/atom" term="cs.DL" scheme="http://arxiv.org/schemas/atom"/>
    <category term="cs.DL" scheme="http://arxiv.org/schemas/atom"/>
  </entry>
</feed>

How can I construct a comma-separate string that contains all values in author/name tags? In the XML above I want to get Pablo Dorta-González, Yolanda Santana-Jiménez

Upvotes: 0

Views: 332

Answers (1)

Wazeed
Wazeed

Reputation: 1280

It is possible to use DOM queries to get the parsed values. Please check below code snippet.

fetch("https://export.arxiv.org/api/query?id_list=1804.10436")
    .then(response => response.text())
    .then(str => new window.DOMParser().parseFromString(str, "text/xml"))
    .then(xml => Array.from(xml.querySelectorAll('author>name')).map(e => e.textContent).join(", "))
    .then(console.log);

Upvotes: 1

Related Questions