2014-02-08 101 views
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">&lt;span class="qt0"&gt;Asthma&lt;/span&gt;</content> 
      <content name="organizationName">National Library of Medicine</content> 
      <content name="altTitle">Bronchial &lt;span class="qt0"&gt;Asthma&lt;/span&gt;</content> 
      <content name="FullSummary">&lt;p&gt;&lt;span class="qt0"&gt;Asthma&lt;/span&gt; is a chronic disease that affects your airways. Your airways are tubes that carry air in and out of your lungs. If you have &lt;span class="qt0"&gt;asthma&lt;/span&gt;, 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.&lt;/p&gt;&lt;p&gt;Symptoms of &lt;span class="qt0"&gt;asthma&lt;/span&gt; include&lt;/p&gt;&lt;ul&gt;&lt;li&gt;Wheezing&lt;/li&gt;&lt;li&gt;Coughing, especially early in the morning or at night&lt;/li&gt;&lt;li&gt;Chest tightness&lt;/li&gt;&lt;li&gt;Shortness of breath&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Not all people who have &lt;span class="qt0"&gt;asthma&lt;/span&gt; have these symptoms. Having these symptoms doesn't always mean that you have &lt;span class="qt0"&gt;asthma&lt;/span&gt;. Your doctor will diagnose &lt;span class="qt0"&gt;asthma&lt;/span&gt; based on lung function tests, your medical history, and a physical exam. You may also have allergy tests.&lt;/p&gt;&lt;p&gt;When your &lt;span class="qt0"&gt;asthma&lt;/span&gt; symptoms become worse than usual, it's called an &lt;span class="qt0"&gt;asthma&lt;/span&gt; attack. Severe &lt;span class="qt0"&gt;asthma&lt;/span&gt; attacks may require emergency care, and they can be fatal.&lt;/p&gt;&lt;p&gt;&lt;span class="qt0"&gt;Asthma&lt;/span&gt; is treated with two kinds of medicines: quick-relief medicines to stop &lt;span class="qt0"&gt;asthma&lt;/span&gt; symptoms and long-term control medicines to prevent symptoms. &lt;/p&gt;&lt;p&gt;NIH: National Heart, Lung, and Blood Institute&lt;/p&gt;</content> 

... ...

+1

請參閱[FindElementAttribute] (http://stackoverflow.com/questions/3024276/find-xml-element-by-attribute) –

+0

嗨西迪克 - 你堅持它應該是:var topicid = $(xml).find('nlmSearchResult> content name = 「標題」「)文本(); – user2110655

+0

$(xml).find('content [name =「title」]')。text() –

回答

1

它看起來像你有兩個問題

第一在這條線上:

$(xml).find('detaildata').each(function(){ 

我在您的XML中看不到任何detaildata元素。這意味着什麼都不會被選中,並且.each()沒有任何東西可以迭代,因此您期望將內容附加到$('#detaildata')的匿名函數並不會被調用。

的第二個問題是,您還需要調整你如何找到具有屬性值使用Attribute Equals Selector元素:

$(xml).find('content[name="title"]') 

試試這個:

$.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(''); 

     // 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>" 
      ); 
    });