如何将表附加到xml文件?

时间:2016-02-11 11:36:30

标签: javascript xml html5 xml-parsing

我希望能够将包含一组属性的整个表附加到xml文件中,以便稍后检索它们。我面临的唯一问题是写入XML文件,而当我对存储的值调用alert()时,它们设置正确。

这是我的代码:

  function getXML(positionX, positionY, canvasWidth, canvasHeight) {
        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            xmlParse(xhttp);
            addToXml(xhttp, positionX, positionY, canvasWidth, canvasHeight);

        };
        xhttp.open("GET", "index.xml", true);
        xhttp.send();
    };




     function addToXml(xml, positionX, positionY, canvasWidth, canvasHeight) {
            var xmlFile = xml.responseXML;
            var table = xmlFile.createElement("table"),
                x = xmlFile.createAttribute("positionX"),
                y = xmlFile.createAttribute("positionY"),
                width = xmlFile.createAttribute("tableWidth"),
                height = xmlFile.createAttribute("tableheight");
            x.nodeValue = positionX;
            y.nodeValue = positionY;
            width.nodeValue = canvasWidth;
            height.nodeValue = canvasHeight;


            table.setAttributeNode(x);
            table.setAttributeNode(y);
            table.setAttributeNode(width);
            table.setAttributeNode(height);

            xmlFile.getElementsByTagName('Tables')[0].appendChild(table);
            alert(xmlFile.getElementsByTagName('table')[0].getAttribute('positionX'));

        }

0 个答案:

没有答案