简体   繁体   中英

Parse child child XML nodes with javaScript

I'm have a tough time getting the value for a particular node. I'm pulling my XML data from the url http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=25092882,25260646,25242549&retmode=xml and I'm using the following code

function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4 && xhttp.status == 200) {
        myFunction(xhttp);
    }
};
xhttp.open("GET",
    "http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=25092882,25260646,25242549&retmode=xml",
    true);
xhttp.send();}

function myFunction(xml) {
var txt = '';
var i;
var affiliation;
var aff;
var pmcid = '';
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName("PubmedArticle");
var authors = "";
for (i = 0; i < x.length; i++) {
    var pmid = 'PMID: ' + x[i].getElementsByTagName("PMID")[0].childNodes[
        0].nodeValue;
    txt += pmid + "</br> ";
    var author = x[i].getElementsByTagName("Author");
    for (a = 0; a < author.length; a++) {
        authors += author[a].getElementsByTagName("LastName")[0].childNodes[
            0].nodeValue;
        authors += " ";
        authors += author[a].getElementsByTagName("Initials")[0].childNodes[
            0].nodeValue;
        affiliation = author[a].getElementsByTagName("AffiliationInfo")
        authors += " Author Affiliation: " + affiliation[0].getElementsByTagName(
            "Affiliation")[0].childNodes[0].nodeValue;
        authors += " " + "</br> ";
    }
    txt += authors + " ";
    var articleTitle = 'Article Title: ' + x[i].getElementsByTagName(
        "ArticleTitle")[0].childNodes[0].nodeValue;
    txt += articleTitle + "</br> ";
    var journal = 'Journal Title: ' + x[i].getElementsByTagName("Title")[
        0].childNodes[0].nodeValue;
    txt += journal + "</br> ";
    var yearPub = 'Date Published: ';
    txt += yearPub + "</br> "
    var AbstractText = 'Abstract Text: ' + x[i].getElementsByTagName(
        "AbstractText")[0].childNodes[0].nodeValue;
    txt += AbstractText + "</br> ";
    txt += "PMCID: " + pmcid + "</br> "
    txt += "</br> "
}
document.getElementById("demo").innerHTML += txt;

}

The line I'm having trouble with is the affiliation. The value is inside the Author Node that I'm looping and then there is the AffiliationInfo then Affiliation. If I take the Affiliation information out the function runs fine but I need to get the Affiliation values.

Thanks ahead of time.

Not all Author nodes have Author Affiliation nodes. You will need to check for existence.

   affiliation = author[a].getElementsByTagName("AffiliationInfo")
   if (affiliation.length > 0) {
       authors += " Author Affiliation: " + affiliation[0].getElementsByTagName("Affiliation")[0].childNodes[0].nodeValue;
       authors += " " + "</br> ";
    }

Set Month to a variable and check if the length.

var pubMonth = [add code to get month]
if (pubMonth.length > 0) {
    '..Do stuff
}

If you really wanted to get serious with XML parsing, I would suggest using XPath. There is a lot of extra code you're writing just to get node values and traverse the tree.

https://developer.mozilla.org/en/docs/Web/API/Document/evaluate

If you don't mind getting into a library that removes alot of the headache, JQuery does it nicely.

https://api.jquery.com/jQuery.parseXML/

Here's a little example with JsJaxy( https://github.com/riversun/JsJaxy ). It's easy to parse XML(xml document) and convert it into JavaScript object.

var xmlParser = new org.riversun.jsjx.XmlParser();

var xhr = new XMLHttpRequest();
xmlParser.addArrayElementName('PubmedArticleSet.PubmedArticle');
xmlParser.addArrayElementName('PubmedArticleSet.PubmedArticle.CommentsCorrectionsList.CommentsCorrections');


xhr.open('GET', 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=25092882,25260646,25242549&retmode=xml', true);
xhr.onreadystatechange = function () {

    if (xhr.readyState == 4) {

        if (xhr.status == 200) {
            var doc = xhr.responseXML;

            //do parse
            var root = xmlParser.parseDocument(doc);

            //show element
            console.log(root.PubmedArticleSet.PubmedArticle[0].MedlineCitation.CommentsCorrectionsList.CommentsCorrections[0].RefSource);
        }
    }

};
xhr.send(null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM