使用Javascript从节点检索所有XML

时间:2016-07-01 17:30:02

标签: javascript xml

我正在尝试从节点获取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]的所有子节点并保留伴随的标签和格式?

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

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;
    responseText = xmlhttp.responseText;
    textNodes = responseText.split(/<subject.*>/);
    textNodes.shift();   //remove first chunk of text
    for (var i = 0; i < textNodes.length; i++) {
        textNodes[i] = textNodes[i].replace(/\r?\n|\r/g, '');   //remove line breaks;
        textNodes[i] = textNodes[i].replace(/^\s*/, '');      // Replace "> " with ">"
        textNodes[i] = textNodes[i].replace(/>\s*/g, '>');      // Replace "> " with ">"
        textNodes[i] = textNodes[i].replace(/\s*</g, '<');      // Replace "< " with "<"
    }
    xmlhttp.onloadend = init(xmlDoc, textNodes);
}

function init(xmlDoc, textNodes) {
    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: textNodes[i] });
    }
    console.log(subjects);
}