2016-12-15 127 views
2

所以,我有這樣的XML我怎樣才能得到第一級的項目?

<conversation> 
    <question> 
     <title>I want to get this parent</title> 
     <question> 
      <title>I don´t want to get this one</title> 
     </question> 
     <question> 
      <title>I don´t want to get this one</title> 
     </question> 
    </question> 

    <question>and get this one</question> 
    <question>and also this one too</question> 
</conversation> 

,我想獲得在頂級水平,直接從<conversation>下降,而不是從任何其他標記降...我怎樣才能得到只有3個<question>節點?

+0

那麼您目前如何訪問數據? – Sirko

+0

這就是我正在做的事情......我已經加載了xml文件,並且我有了整個xml樹,現在我需要獲得這3個節點,仍然閱讀文檔,但我沒有看到任何關於該文檔的信息(obv。我錯過了那部分,很確定它是「那裏」) – torpedete

+0

你如何「擁有整棵樹」?假設XML被解析爲一個對象,所有你需要的東西就像'var qs = xml [0] [0];''或'var title = xml.conversation [0] .question;' –

回答

6

document.querySelectorAll('conversation > question')

的>表示直接孩子。

example here只所以沒有困惑:

console.log( 
 
\t document.querySelectorAll('conversation > question') 
 
)
<conversation> 
 
    <question> 
 
    <title>I want to get this parent</title> 
 
    <question> 
 
     <title>I don´t want to get this one</title> 
 
    </question> 
 
    <question> 
 
     <title>I don´t want to get this one</title> 
 
    </question> 
 
    </question> 
 

 
    <question>and get this one</question> 
 
    <question>and also this one too</question> 
 
</conversation>

+0

第一場比賽將包含子節點''節點? – guest271314

+0

是的,作爲一個孩子的財產。 – rlemon

+0

OP在詢問如何不返回嵌套的子節點。您的解決方案不會過濾來自'.querySelectorAll()' – guest271314

0

我不確定這是否是你需要的,但你把JavaScript標籤。 因此,這裏是一個JavaScript代碼,這將讓你的所有節點:

document.getElementsByTagName('question'); 
+0

我不想要所有節點,只有頂級的節點,直接從降序 – torpedete

+0

因此,只需從列表中獲取第一個元素:var firstQuestion = document.getElementsByTagName('question')[0]; –

0

試試這個讓談話

的第一級子
document.getElementsByTagName('conversation')[0].children 
0

您可以使用document.querySelectorAll()與選擇"conversation question"Array.from(),檢查當前元素.parentElement.nodeName"QUESTION",如果返回true空字符串,否則檢查元素是否具有.firstElementChild.firstElementChild.nodeName"TITLE",如果爲true,則返回.firstElementChild.textContent,否則返回.firstChild.textContent,在生成的文本上調用.trim(),鏈.filter()Boolean作爲參數刪除空字符串。

var questions = Array.from(
 
    document.querySelectorAll("conversation question") 
 
    , function(el) { 
 
    return (el.parentElement.nodeName === "QUESTION" 
 
      ? "" 
 
      : el.firstElementChild 
 
       && el.firstElementChild.nodeName === "TITLE" 
 
       ? el.firstElementChild.textContent 
 
       : el.firstChild.textContent).trim(); 
 
    }).filter(Boolean); 
 

 
console.log(questions);
<conversation> 
 
    <question> 
 
    <title>I want to get this parent</title> 
 
    <question> 
 
     <title>I don´t want to get this one</title> 
 
    </question> 
 
    <question> 
 
     <title>I don´t want to get this one</title> 
 
    </question> 
 
    </question> 
 

 
    <question>and get this one</question> 
 
    <question>and also this one too</question> 
 
</conversation>

+0

OP詢問如何獲取節點,而不是文本內容。你已經完成了複雜的事情。 – rlemon

+0

@rlemon _「你想獲取選定節點的文本嗎?」_ http:// stackoverflow。我只能得到這個項目在第一級/ 41164222?noredirect = 1#comment69527331_41163246,_「Yep」_ http://stackoverflow.com/questions/41163246 /如何-可以-I-GET-僅-的項,在優先級/ 41164222?noredirect = 1個#comment69527362_41163246。 – guest271314

+1

你正在選擇和解釋,OP特別提到在其他地方收集節點。 – rlemon

1

如果你想要的是頂級<question>元素,@rlemonanswer是一個你需要的。

如果你想從這些元素的文本,你需要擴大自己的代碼一點點:

console.log(
 
    Array.prototype.slice.call(       // Convert the `querySelectorAll` result to an array 
 
    document.querySelectorAll('conversation > question') // Get all top question nodes 
 
).map(e => e.innerText || e.children[0].textContent) // Get the element's text, or the element's first child's text. 
 
)
<conversation> 
 
    <question> 
 
    <title>I want to get this parent</title> 
 
    <question> 
 
     <title>I don´t want to get this one</title> 
 
    </question> 
 
    <question> 
 
     <title>I don´t want to get this one</title> 
 
    </question> 
 
    </question> 
 

 
    <question>and get this one</question> 
 
    <question>and also this one too</question> 
 
</conversation>

+0

改變你的觀點,側翼隊友,優雅的解決方案或以上所有? – guest271314

+2

@ guest271314:不完全。我仍然認爲他只是想要的元素,但如果他想要的文字,你的答案不完全是最優雅的方式來做到這一點。 – Cerbrus

+0

在這張紙條上不能不同意你的意見。希望徹底並顯示過程,以便OP可以通過檢查'.parentElement','.filterElementChild'和'.nodeName's來過濾其他元素。一點都不優雅。也許OP會加入。現在肯定有答案。 – guest271314