2016-06-14 34 views
0

我們如何限制查找僅在XML文檔中的第一級兒童中搜索。jQuery:僅在第一級兒童中查找(XML)

例如:

考慮這個XML是從Web服務人子(https://services.onetcenter.org/developer/

<?xml version="1.0" encoding="UTF-8"?> 
<report code="27-2011.00"> 
    <career> 
     <code>13-2011.01</code> 
     <title>Accountants</title> 
     <tags bright_outlook="true" green="false" apprenticeship="false"/> 
     <also_called> 
      <title>Accountant</title> 
      <title>Accounting Manager</title> 
      <title>Certified Public Accountant (CPA)</title> 
      <title>Staff Accountant</title> 
     </also_called> 
     <what_they_do>Analyze financial information and prepare financial reports to determine or maintain record of assets, liabilities, profit and loss, tax liability, or other financial activities within an organization.</what_they_do> 
     <on_the_job> 
      <task>Prepare, examine, or analyze accounting records, financial statements, or other financial reports to assess accuracy, completeness, and conformance to reporting and procedural standards.</task> 
      <task>Report to management regarding the finances of establishment.</task> 
      <task>Establish tables of accounts and assign entries to proper accounts.</task> 
     </on_the_job> 
     <resources> 
      <resource href="https://services.onetcenter.org/ws/mnm/careers/13-2011.01/knowledge"> 
       <title>Knowledge</title> 
      </resource> 
.......................... 
.......................... 
............................. 
    </explore_more> 
    <where_do_they_work> 
     <industry href="https://services.onetcenter.org/ws/mnm/browse/54" percent_employed="33"> 
      <code>54</code> 
      <title>Professional, Science, &amp; Technical</title> 
     </industry> 
    </where_do_they_work> 
</report> 

在這裏,我想對佈局模式對話框這個數據的響應。所以爲了這個目的,我需要處理每個節點。

所以首先我需要找到標題。所以我有這個代碼我嘗試過。

var career = $(xml).find('career'); 

然後我給你使用

$('#myh4').html($(career).find('title').text()); 

冠軍標籤的文本內容到標題字段

就這樣我編寫它,但顯然它分配的所有標題標籤文字的頭。

所以簡單的問題是我們如何才能將查找功能限制在一級兒童身上。意味着如何防止它被挖到更高層次兒童喜歡also_called

回答

1

使用.children('title').text()instead

()方法從.find()在。兒童(不同)僅 行進的單一電平向下DOM樹而.find()可以遍歷 向下多個級別來選擇後代元素的。兒童(孫子, 等)。