xml元素作为对象HTMLCollection返回

时间:2021-11-28 07:37:52

I have an xml document that returns a list of map markers.

我有一个xml文档,它返回一个地图标记列表。

<markers>
<marker name="Marker 1 name" theid="100">
<content>Text goes here</content>
</marker>
<marker name="Marker 2 name" theid="101">
<content>Other text goes here</content>
</marker>
...
</markers>

I have some javascript to read through the list of markers and it successfully returns their attributes as variables like name and theid.

我有一些javascript来读取标记列表,它成功地将其属性作为名称和theid等变量返回。

<script>
...
var xml = parseXml(data);
var markerNodes = xml.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markerNodes.length; i++) {
var name = markerNodes[i].getAttribute("name");
var theid = markerNodes[i].getAttribute("theid");
var content = markerNodes[i].getElementsByTagName("content");
...
</script>

However I cannot get the javascript to return the contents of the element tag content. In place of the text content I get the message [object HTMLCollection]. Would anyone be kind enough to help me fix this please?

但是我无法获取javascript来返回元素标记内容的内容。代替文本内容,我得到消息[object HTMLCollection]。有人愿意帮我解决这个问题吗?

1 个解决方案

#1


2  

The bit I was missing was textContent:

我缺少的是textContent:

var content = markerNodes[i].getElementsByTagName("content")[0].textContent;

var content = markerNodes [i] .getElementsByTagName(“content”)[0] .textContent;

#1


2  

The bit I was missing was textContent:

我缺少的是textContent:

var content = markerNodes[i].getElementsByTagName("content")[0].textContent;

var content = markerNodes [i] .getElementsByTagName(“content”)[0] .textContent;