2016-09-15 10 views
0

jQueryからXMLを読み取る際に問題があります。jQueryを使用してXMLを読み取ることができません

$.ajax({ 
     type: "GET", 
     contentType: "application/json; charset=utf-8", 
     cache: false, 
     url: baseUrl + "/sitefinity/services/tafesa/TAFEpagedata.svc/Mainmenu", 
     data: {}, 
     dataType: 'xml', 
     success: function (data, textStatus, jqXHR) { 

     console.log(data); 

      $(data).find('menuitem').each(function() { 

         htmlString = htmlString + writeToplevelNav($(this).attr('title'), $(this).attr('url'),$(this)); 
      }); 

そして、これが私のXMLです::

<GetMainNavResponse xmlns="http://tempuri.org/"> 
    <GetMainNavResult> 
     <mainMenu> 
      <mainitem title="Courses" url="~/courses"> 
       <subitem title="Primary Industries @amp; Science" url="~/courses/primary-ind-science"> 
        <item title="Agriculture" url="~/courses/primary-ind-science/agriculture"/> 
        <item title="Animal Care @amp; Veterinary Nursing" url="~/courses/primary-ind-science/animal-care-veterinary-nursing"/> 
        <item title="Aquaculture" url="~/courses/primary-ind-science/aquaculture"/> 
        <item title="Conservation @amp; Land Management" url="~/courses/primary-ind-science/conservation-land-management"/> 
        <item title="Horticulture" url="~/courses/primary-ind-science/horticulture"/> 
        <item title="Science" url="~/courses/primary-ind-science/laboratory-technology"/> 
       </subitem> 
      </mainitem> 
     </mainMenu> 
    </GetMainNavResult> 
</GetMainNavResponse> 

私が最初にして、その属性を読んで、私のコードを「メニューアイテムを」読みたかった私は、XMLを読み込むことができますが、私はそのタグを読み取ることができません誰もがこれで多くのおかげで私を助けることができます!

$(data).find('mainitem') 

あなたはタイトルコースとURL〜/コース

ある最初の要素を取得します:

答えて

0

問題は、あなたがあなたがあなたのコードを変更する場合はポストされたXML内のmenuitemと呼ばれる要素がないですその後そののMainMenu mainitemのために私はあなたがそれは同様経由でサブ項目です取得したいと思うだろう:

$(data).find('mainitem').each(function() { 
    $(this).find('subitem').each(function() { 
     // Do some processing with subitem 
    }); 

    // Do some processing with mainitem as well 
}); 

そしてサブ項目項目:

$(data).find('mainitem').each(function() { 
    $(this).find('subitem').each(function() { 

     $(this).find('item').each(function() { 
      // Process subitem's items 
     }); 

     // Do some processing with subitem as well 
    }); 

    // Do some processing with mainitem as well 
}); 

私はこのテクニックを示す溶液でここにgithubのプロジェクトを作成しました: https://github.com/davethomas11/stackoverflow_Q_39502601

主催例: https://www.daveanthonythomas.com/remote/so39502601/

+0

こんにちはデイブ、迅速な解決のための 感謝を。はい私は間違って別の名前を参照していますが、私はURLを置き換えるとき、それは問題ではありませんでした: "URL:baseUrl +"/sitefinity/services/tafesa/menu.xml "、"これは動作します。 これを私のバックエンド開発者に確認する必要があります。 あなたの答えをよろしくお願いします。私の質問によれば、それは正しいです! – NPN

関連する問題