【发布时间】:2016-07-01 17:30:01
【问题描述】:
我正在尝试从节点获取 xml。假设我有一个 XML 文件:
<?xml version="1.0"?>
<story title="My title here">
<subject key="key1" caption="Intro">
Text here for subject 1. There might be an occasional <html> markup present.
<action tag="actiontag"/>
</subject>
<subject key="key2" caption="Chap1">
Text for chapter 2
<directions>
<dir go="chap5" to="Solving"/>
<dir go="chap12" to="Searching">
<extra1 subtitle="subtitle">You can expect extra text here as well.</extra>
<extra2 subtitle="subtitle2"/>
</dir>
<dir go="chap2,chap5" to="Finding"/>
</directions>
</subject>
<chapters key="chap1" caption="Chapter1">
The text for chapter1 goes here
<newtag>This one has text as well</newtag>
</chapters>
</story>
现在我正在尝试将包括节点和标签在内的整个 XML 代码放入对象数组中。所以理想的结果应该是:
subjects[0].key=key1
subjects[0].caption=Intro
subjects[0].txt=Text here for subject 1. There might be an occasional <html> markup present.<action tag="actiontag"/>
subjects[1].key=key2
subjects[1].caption=Chap1
subjects[1].txt=Text for chapter 2<directions><dir go="chap5" to="Solving"/><dir go="chap12" to="Searching"><extra1 subtitle="subtitle">You can expect extra text here as well.</extra><extra2 subtitle="subtitle2"/></dir><dir go="chap2,chap5" to="Finding"/></directions>
此“文本”稍后可以作为 XML 处理。 现在我已经能够分别读取 XML 和访问标签了。我已经能够遍历文件并获取文本,但我似乎无法遍历所有节点/文本/标签并保持其格式不变。
我拥有的是:
var xmlDoc;
function loadxml() {
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET", "assets/myfile.xml", false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML;
xmlhttp.onloadend = init(xmlDoc);
}
function init(xmlDoc) {
var subjects = [];
var x, i;
x = xmlDoc.getElementsByTagName('subject');
for (i = 0; i < x.length; i++) {
subjects.push({ key: x[i].getAttribute('key'), caption: x[i].getAttribute('caption'), txt: x[i].childNodes[0].nodeValue });
}
//just to check if there's something recorded..
document.getElementById("result").innerHTML = subjects[1].txt;
}
对象数组没问题,可以。但是如何更改 x[i].childNodes[0].nodeValue 以保存 [subject] 的所有子节点并保留随附的标签和格式?
感谢您的宝贵时间。
【问题讨论】:
-
不是您的问题的答案,但您真的需要 IE6 及以下版本的代码吗?
-
使用 jQuery 可能更容易,试试 $.parseXML()
-
他已经有一个解析过的 XML 文档,@derloopkat,来自
xmlhttp.responseXML。 -
你打算如何使用
subject的值?因为如果您只是要对其进行进一步解析,请使用txt: x[i].childNodes[0]以便您有一个可以使用的节点。如果要将其附加到 DOM,请使用appendChild(subjects[1].txt)而不是innerHTML。 -
@Mike McCaughan:不需要 IE6 及以下版本。这是从网络上快速复制和粘贴的。不确定如何处理子节点。最终我需要其中的标签并根据标签的内容进行处理。我目前正在努力的是清楚地了解如何处理 childNodes。我的第一个想法是将 .txt 作为 XML 文档从字符串处理。
标签: javascript xml