2016-12-15 61 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