1
我是新來的XML和卡住,希望有人可以建議。只顯示標題,但不顯示任何子項目。我認爲我的錯誤在這裏:解析XML - 子項目不被解析
var topicid = $(xml).find('content name="title"').text();
有沒有人有一些想法?
的JS是:
$.ajax({
type: "GET",
url: "http://wsearch.nlm.nih.gov/ws/query?db=healthTopics&term=asthma",
dataType: "xml",
cache: false,
success: function(xml) {
// Get header data
var name = $(xml).find('nlmSearchResult').text();
$('#headername').html('<b>' + name + '</b>');
// Reset detail div
$('#detaildata').text('');
// Loop through the detaildata and find all prod nodes
$(xml).find('detaildata').each(function(){
// Move detail data into internal variables
var topicid = $(xml).find('content name="title"').text();
var fullsummary = $(xml).find('content name="FullSummary"').text();
var alsocalled = $(xml).find('content name="altTitle"').text();
$('#detaildata').append(
"<b>" + topicid + "</b>" + " - "
+ "Also known as: " + alsocalled + " - "
+ fullsummary
+ "<br>"
);
});
},
的XML是:
<?xml version="1.0" encoding="UTF-8"?>
<nlmSearchResult>
<term>asthma</term>
<file>viv_u1cZOb</file>
<server>pvlbsrch15</server>
<count>65</count>
<retstart>0</retstart>
<retmax>10</retmax>
<list num="65" start="0" per="10">
<document rank="0" url="http://www.nlm.nih.gov/medlineplus/asthma.html">
<content name="title"><span class="qt0">Asthma</span></content>
<content name="organizationName">National Library of Medicine</content>
<content name="altTitle">Bronchial <span class="qt0">Asthma</span></content>
<content name="FullSummary"><p><span class="qt0">Asthma</span> is a chronic disease that affects your airways. Your airways are tubes that carry air in and out of your lungs. If you have <span class="qt0">asthma</span>, the inside walls of your airways become sore and swollen. That makes them very sensitive, and they may react strongly to things that you are allergic to or find irritating. When your airways react, they get narrower and your lungs get less air.</p><p>Symptoms of <span class="qt0">asthma</span> include</p><ul><li>Wheezing</li><li>Coughing, especially early in the morning or at night</li><li>Chest tightness</li><li>Shortness of breath</li></ul><p>Not all people who have <span class="qt0">asthma</span> have these symptoms. Having these symptoms doesn't always mean that you have <span class="qt0">asthma</span>. Your doctor will diagnose <span class="qt0">asthma</span> based on lung function tests, your medical history, and a physical exam. You may also have allergy tests.</p><p>When your <span class="qt0">asthma</span> symptoms become worse than usual, it's called an <span class="qt0">asthma</span> attack. Severe <span class="qt0">asthma</span> attacks may require emergency care, and they can be fatal.</p><p><span class="qt0">Asthma</span> is treated with two kinds of medicines: quick-relief medicines to stop <span class="qt0">asthma</span> symptoms and long-term control medicines to prevent symptoms. </p><p>NIH: National Heart, Lung, and Blood Institute</p></content>
... ...
請參閱[FindElementAttribute] (http://stackoverflow.com/questions/3024276/find-xml-element-by-attribute) –
嗨西迪克 - 你堅持它應該是:var topicid = $(xml).find('nlmSearchResult> content name = 「標題」「)文本(); – user2110655
$(xml).find('content [name =「title」]')。text() –