2016-08-14 137 views
0

我很難獲得特定節點的值。我是從網址http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=25092882,25260646,25242549&retmode=xml拉着我的XML數據,我用下面的代碼用javaScript解析子節點XML節點

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; 

}

我遇到的麻煩的直線是隸屬關係。該值位於正在循環的作者節點中,然後是AffiliationInfo,然後是隸屬關係。如果我將聯盟信息取出,該函數運行良好,但我需要獲取聯盟值。

提前致謝。

回答

1

並非所有的Author節點都有Author Affiliation節點。你需要檢查是否存在。

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

設置Month給一個變量,並檢查長度。

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

如果你真的想認真與XML解析,我會建議使用XPath。爲了獲取節點值並遍歷樹,有很多額外的代碼正在編寫。

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

如果你不介意到庫,消除了很多頭痛的,jQuery也好聽。

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

+0

在那裏工作很好。在PubDate部分的月份如何。我可以得到一年,但不是一個月? v = x [i] .getElementsByTagName(「PubDate」); y + = v [0] .getElementsByTagName(「Year」)[0] .childNodes [0] .nodeValue;對不起,我不知道如何在評論中添加代碼塊 – user1314159

+0

我明白了,有些PubDate記錄沒有月份,所以它不會運行。如果我只處理年份和月份的記錄,它可以正常工作。我如何測試Month是否存在。 – user1314159

+0

謝謝 - 我得到了我需要的一切,現在我需要努力提高速度。我想拉一百份出版物,然後立即進行解析。我將使用javaScript XML過程作爲我的初始基準,現在嘗試jQuery並查看更快。 D你有什麼建議可以產生最快的結果? – user1314159

0

下面是與JsJaxy(https://github.com/riversun/JsJaxy)一個小例子。 很容易解析XML(xml文檔)並將其轉換爲JavaScript對象。

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