我想从通过 XMLHttpRequest 获取的大型 XML 文档中检索元素。响应非常大,因此将其添加到显示设置为“无”的文档/窗口/网站,然后在主文档上执行 document.querySelectorAll("sometag") 是不可行的。
目前,我有这个代码:
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
function fetchIds(){
var url = "something";
let request = new XMLHttpRequest();
request.open("POST", url);
request.send();
request.onload = function(){
var ids = request.responseText;
ids = ids.replace(/[<>]|(Id)|(List)|\//gi, "").trim().split("\n").join(",");
return ids;
}
}
fetchIds();
当它返回时,我得到这样的文件:
<AuthorList CompleteYN="Y">
<Author ValidYN="Y">
<LastName>Idziak-Helmcke</LastName>
<ForeName>Dominika</ForeName>
<Initials>D</Initials>
<Identifier Source="ORCID">0000-0002-0713-2466</Identifier>
<AffiliationInfo>
<Affiliation>Institute of Biology, Biotechnology, and Environmental Sciences, University of Silesia in Katowice, Jagiellońska 28, 40-032 Katowice, Poland.</Affiliation>
</AffiliationInfo>
</Author>
<Author ValidYN="Y">
<LastName>Warzecha</LastName>
<ForeName>Tomasz</ForeName>
<Initials>T</Initials>
<Identifier Source="ORCID">0000-0002-7121-0123</Identifier>
<AffiliationInfo>
<Affiliation>Department of Plant Breeding, Physiology, and Seed Science, University of Agriculture in Kraków, Podłużna 3, 30-239 Kraków, Poland.</Affiliation>
</AffiliationInfo>
</Author>
<Author ValidYN="Y">
<LastName>Sowa</LastName>
<ForeName>Marta</ForeName>
<Initials>M</Initials>
<AffiliationInfo>
<Affiliation>Institute of Biology, Biotechnology, and Environmental Sciences, University of Silesia in Katowice, Jagiellońska 28, 40-032 Katowice, Poland.</Affiliation>
</AffiliationInfo>
</Author>
为了使这个问题变得简单,我想获取此 XML 文档中的所有 LastName 标记,但我想直接从 request.responseText 或 request.responseXML 中执行此操作。这可以用 JavaScript 实现吗?我试过做 request.responseXML.querySelectorAll("sometag"),但没有运气。
任何帮助将不胜感激,谢谢!