【问题标题】:Javascript get all tag values with the same tag nameJavascript获取具有相同标签名称的所有标签值
【发布时间】:2021-07-10 13:17:16
【问题描述】:

我有下面的 javascript 来获取 XML 提要:

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

XML 如下所示:

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

如何构造一个逗号分隔的字符串,其中包含author/name 标签中的 all 值?在上面的XML中我想得到Pablo Dorta-González, Yolanda Santana-Jiménez

【问题讨论】:

    标签: javascript xml domparser


    【解决方案1】:

    可以使用 DOM 查询来获取解析后的值。请检查下面的代码sn-p。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 2021-08-14
      • 2013-11-11
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多