在XML中,有没有办法将节点的所有子节点转换为单个字符串,包括嵌套的XML标记?



我在XML文件中有如下内容,该文件是仅使用CSS和javascript的HTML页面的数据源。特殊的XML代码是我自己的,我想用javascript处理它们。

<listitem>regular text could be in here</listitem>
<listitem>possibly with <b>HTML markup</b></listitem>
<listitem>or <special>special xml</special></listitem>

我梦想的是一种从。getelementsbytagname ("listitem")到以下数组的方法。

["regular text could be in here", "possibly with <b>HTML markup</b>", "or <special>special xml</special>"]
这样,我就可以将每个列表项作为数组的一部分来处理。但是,XML解析器会分解每个列表项的所有XML。除了使用CDATA之外,还有其他方法吗?

我想答案是:

Array.prototype.slice.call(document.getElementsByTagName("listitem")).map(function(x) {return x.innerHTML})

它将返回:

["regular text could be in here", "possibly with <b>HTML markup</b>", "or <special>special xml</special>"]

最新更新