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
Javascript/XML - Getting the node name
提问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 (Sorry about that, for tagName
, which is the name of the element.Element
s, tagName
and nodeName
are the same.)
您想要(很抱歉,对于tagName
,这是元素的名称。Element
s,tagName
和nodeName
是一样的。)
The problem is that the first child of your myParent
element isn't the myChild
element, 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 myChild
element, which you can do with getElementsByTagName
again, 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 Element
s don't have a meaningful nodeValue
property; instead, you collect their child text nodes. (More in the DOM specs: DOM2, DOM3.)
请注意,Element
s 没有有意义的nodeValue
属性;相反,您收集它们的子文本节点。(更多在 DOM 规范中:DOM2,DOM3。)
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 数据。我没有使用过任何其他的,比如Prototype,YUI,Closure,或者其他几个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 可信,如果直接子节点包含更多元素节点,则其他索引可能返回不是子节点的元素。)