解析xml - 未解析的子项

时间:2014-02-08 18:14:11

标签: jquery 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 个答案:

答案 0 :(得分: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>"
            );
    });