2

これは別の質問に関連していますが、重複していません。それは私が行き詰まりに達したという提案された解決策を扱っています。

XML を読み取り、変更を加え、ウィンドウを開き、XML をドキュメントに書き込む次のコードがあります。問題は、コンテンツが XML としてレンダリングされないことです。ブラウザがコンテンツを XML として処理するようにコンテンツ タイプなどを設定する方法はありますか?

<script>
var wxml;
var xDoc;
var xDevices, xInputs;
var xDevice, xInput;

    function fSetXmlAInput(iDevice, iNode, nodeNewVal) {
      xInput = xInputs[iNode];
      xValue = xInput.getElementsByTagName("value")[0];

      // change node value:
      // console.log("nodeVal: " + xValue.firstChild.nodeValue);
      xValue.firstChild.nodeValue = nodeNewVal;
      // console.log("newVal: " + xValue.firstChild.nodeValue);
    }

    function fSetXmlDevice(iDevice) {
      xDevice = xDevices[iDevice];
      xInputs = xDevice.getElementsByTagName("input");
        fSetXmlAInput(iDevice, 0, "22");
        fSetXmlAInput(iDevice, 1, "33");
    }

    function alternativeLoadXML3() {
        // load xml file
        if (window.XMLHttpRequest) {
            xhttp = new XMLHttpRequest();
        } else { // IE 5/6
            xhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xhttp.open("GET", "my_template.xml", false);
        xhttp.send();

        xDoc = xhttp.responseXML;
        xDevices = xDoc.getElementsByTagName("device");
        fSetXmlDevice(1);

        var xmlText = serializeXmlNode(xDoc);

        var newWindow = window.open("my_template.xml", "Test", "width=300,height=300,scrollbars=1,resizable=1");
        newWindow.document.open();
        newWindow.document.write(xmlText);
        newWindow.document.close()
    };

</script>

XMLの下にも:

<?xml version="1.0" encoding="utf-8"?>
<myXmlRoot>
<device>
  <input><name>"name 1"</name><value>{replaceMe!}</value></input>
  <input><name>"name 2"</name><value>{replaceMe!}</value></input>  
</device>
<device>
  <input><name>"name 1"</name><value>{replaceMe!}</value></input>
  <input><name>"name 2"</name><value>{replaceMe!}</value></input>  
</device>
<device>
  <input><name>"name 1"</name><value>{replaceMe!}</value></input>
  <input><name>"name 2"</name><value>{replaceMe!}</value></input>  
</device>
</myXmlRoot>

ブラウザに新しいウィンドウのコンテンツを XML として強制的にレンダリングさせる方法はありますか? または、document.open と document.write を使用すると、コードが HTML としてレンダリングされることを意味しますか?

関連: JavaScript を使用した XML コンテンツの変更、更新の欠落

4

2 に答える 2

4

dataURI を使用して xml を新しいウィンドウに書き込むのは非常に簡単です。

window.open('data:text/xml,'+encodeURIComponent(xmlText),
     "Test", "width=300,height=300,scrollbars=1,resizable=1");
于 2014-10-11T10:04:54.943 に答える