Javascript/XML - 获取节点名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5897315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 18:43:48  来源:igfitidea点击:

Javascript/XML - Getting the node name

javascriptxmlparsingdom

提问by bogatyrjov

I need to get the the name of the tag "myChild" and the "content". This is simple, but i am stuck, sleepy and here is what I get with my tests:

我需要获取标签“myChild”和“内容”的名称。这很简单,但我被困住了,困了,这是我的测试结果:

XML:

XML:

...
<myParent>
  <myChild>content</myChild>
</myParent>
<myParent>
  <myChild>content</myChild>
</myParent>
...

JS:

JS:

var x=xmlDoc.getElementsByTagName("myParent");
alert(x[1].childNodes[0].nodeName); //returns "#text" - "myChild" needed
alert(x[1].childNodes[0].nodeValue); //returns "" - "content" needed

回答by T.J. Crowder

You want tagName, which is the name of the element.(Sorry about that, for Elements, tagNameand nodeNameare the same.)

您想要tagName,这是元素的名称。(很抱歉,对于Elements,tagNamenodeName是一样的。)

The problem is that the first child of your myParentelement isn't the myChildelement, it's a text node (containing whitespace). Your structure looks like this:

问题是myParent元素的第一个子元素不是myChild元素,而是一个文本节点(包含空格)。您的结构如下所示:

  • Element "myParent"
    • Text node with a carriage return and some spaces or tabs
    • Element "myChild"
      • Text node with "content"
    • Text node with a carriage return and some spaces or tabs
  • Element "myParent"
    • Text node with a carriage return and some spaces or tabs
    • Element "myChild"
      • Text node with "content"
    • Text node with a carriage return and some spaces or tabs
  • 元素“我的父母”
    • 带有回车符和一些空格或制表符的文本节点
    • 元素“我的孩子”
      • 带有“内容”的文本节点
    • 带有回车符和一些空格或制表符的文本节点
  • 元素“我的父母”
    • 带有回车符和一些空格或制表符的文本节点
    • 元素“我的孩子”
      • 带有“内容”的文本节点
    • 带有回车符和一些空格或制表符的文本节点

You need to navigate down to the actual myChildelement, which you can do with getElementsByTagNameagain, or just by scanning:

您需要向下导航到实际myChild元素,您可以getElementsByTagName再次使用它,或者只需扫描:

var x=xmlDoc.getElementsByTagName("myParent");
var c = x[1].firstChild;
while (c && c.nodeType != 1) { // 1 = ELEMENT_NODE
    c = c.nextSibling;
}
alert(c.nodeName); // "myChild"

Note that Elements don't have a meaningful nodeValueproperty; instead, you collect their child text nodes. (More in the DOM specs: DOM2, DOM3.)

请注意,Elements 没有有意义的nodeValue属性;相反,您收集它们的子文本节点。(更多在 DOM 规范中:DOM2DOM3。)

Also note that when indexing into a NodeList, the indexes start at 0. You seem to have started with 1; ignore this comment if you were skipping the first one for a reason.

另请注意,索引到 a 时NodeList,索引从0. 你似乎已经开始了1; 如果您出于某种原因跳过第一个评论,请忽略此评论。



Off-topic: It's always best to understand the underlying mechanics of what you're working with, and I do recommend playing around with the straight DOM and referring to the DOM specs listed above. But for interacting with these trees, a good library can be really useful and save you a lot of time. jQueryworks well with XML data. I haven't used any of the others like Prototype, YUI, Closure, or any of several otherswith XML, so can't speak to that, but I expect at least some of them support it.

题外话:理解你所使用的底层机制总是最好的,我建议你使用直接的 DOM 并参考上面列出的 DOM 规范。但是对于与这些树进行交互,一个好的库会非常有用并为您节省大量时间。jQuery可以很好地处理 XML 数据。我没有使用过任何其他的,比如PrototypeYUIClosure,或者其他几个XML 中的任何一个,所以不能说,但我希望至少其中一些支持它。

回答by Dr.Molle

Try x[1].getElementsByTagName('*')[0]instead.

试试吧x[1].getElementsByTagName('*')[0]

(This is only trustable for index 0, other indexes may return elements that are not child-nodes, if the direct childs contain further element-nodes. )

(这仅对索引 0 可信,如果直接子节点包含更多元素节点,则其他索引可能返回不是子节点的元素。)