0

I want to retrieve elements from a large XML-document acquired through an XMLHttpRequest. The response is quite large so adding it to the document/window/website with display set to "none" and then doing a document.querySelectorAll("sometag") on the main document is not doable.


Currently, I have this code:


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

When this returns I get a document of this sort:


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

For the sake of making this question simple, I want to grab all the LastName tags in this XML-document, but I want to do this directly from the request.responseText or the request.responseXML. Is this possible with JavaScript? I have tried doing request.responseXML.querySelectorAll("sometag"), but with no luck.

Any help would be greatly apprecited, thanks!


lukact
  • 50
  • 11
  • *I have tried doing request.responseXML.querySelectorAll("sometag"), but with no luck* <-- Exactly where did you try this? Did you get an error? Please edit your question to show what you've tried. – Scott Marcus Jun 23 '20 at 00:30
  • @ScottMarcus 2 previous questions have been proposed to me with possible answers to my problem. I'll get back to you if those don't solve my issue, thanks – lukact Jun 23 '20 at 00:32
  • @ScottMarcus Yep, that solved it :D Awesome, do I delete this question now or do I let it persist? – lukact Jun 23 '20 at 01:04
  • Let it stay for others who may have the same question, but add a comment about "what solved it" for you exactly. – Scott Marcus Jun 23 '20 at 01:06
  • Will do, I just gotta get some sleep, it's 4 AM at my time zone – lukact Jun 23 '20 at 01:25

0 Answers0