在 Firefox 附加组件中使用元素和属性创建 XML
Create XML with element and attribute in Firefox add-on
我正在尝试在 Firefox 附加组件中使用 JavaScript 构建一些 XML 并遇到问题。
下面是我要找的XML格式:
<testing>
<testingOne attr="my_attrib">
<TestingTwo>abc</TestingTwo >
<TestingThree>bbc</TestingThree>
</testingOne >
</testing>
我尝试使用下面的代码。但是,它不起作用。
var XML = document.createElement("div");
var Node = document.createElement("testing");
Node.appendChild( document.createElement("testingOne") );
var a = document.createAttribute("my_attrib");
node.setAttributeNode(a);
Node.appendChild( document.createElement("TestingTwo") );
Node.appendChild( document.createElement("TestingThree") );
XML.appendChild(Node);
alert(XML.innerHTML);
如何在 Firefox 附加组件中使用 JavaScript 创建 XML 就像上面的示例一样?
你也可以使用 DOMParser,你可能更喜欢这个:
您想要在 Firefox 附加组件中使用 JavaScript 创建一个 XML DOM 树。在 Firefox 附加组件中这样做比在网页中 运行 形式 JavaScript 更复杂。这样做的原因是 window
和 document
变量不能保证被设置。即使已设置,它们也可能未设置为您期望的样子。 Firefox 附加组件处理多个 windows 和多个文档。因此,如果您使用 window
和 document
变量,您应该始终确保将它们设置为您想要的。
在这种情况下,我们只找到最近使用的 Firefox window 的主要 <window>
元素和与当前显示的选项卡关联的 <document>
元素。
以下代码将生成您想要的 XML:
function createDesiredXMLElements() {
//To create elements, we need a <document>. To find a <document> we need a <window>.
//This gets the currently active Firefox XUL window.
// Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
//* Add-on SDK:
let activeWindow = require('sdk/window/utils').getMostRecentBrowserWindow();
//*/
/* Overlay and bootstrap (from almost any context/scope):
Components.utils.import("resource://gre/modules/Services.jsm");//Services
let activeWindow=Services.wm.getMostRecentWindow("navigator:browser");
//*/
//Get the HTML document for the currently displayed tab in the current Firefox window.
let contentDoc = activeWindow.content.document;
//To create XML elements, we need an XML document. We could do it without creating
// an XML document. But, then we would need to use createElementNS() to specify
// the XML namespace for each element. This way, they inherit the xmlDoc's namespace.
let xmlNS = "http://www.w3.org/XML/1998/namespace";
let xmlDoc = contentDoc.implementation.createDocument(xmlNS,"document");
//Create the XML elements/nodes/attribute and add text.
let testing = xmlDoc.createElement("testing");
let testingOne = xmlDoc.createElement("testingOne");
testingOne.setAttribute("attr","my_attrib");
let testingTwo = xmlDoc.createElement("TestingTwo");
testingTwo.textContent = "abc";
let testingThree = xmlDoc.createElement("TestingThree");
testingThree.textContent = "bbc";
//Place the elements as children of each appropriate node.
testing.appendChild(testingOne);
testingOne.appendChild(testingTwo);
testingOne.appendChild(testingThree);
//Show the alert. Note that the alert function is a method of a <window>:
activeWindow.alert(testing.outerHTML);
}
此代码生成的警报如下所示:
我正在尝试在 Firefox 附加组件中使用 JavaScript 构建一些 XML 并遇到问题。
下面是我要找的XML格式:
<testing>
<testingOne attr="my_attrib">
<TestingTwo>abc</TestingTwo >
<TestingThree>bbc</TestingThree>
</testingOne >
</testing>
我尝试使用下面的代码。但是,它不起作用。
var XML = document.createElement("div");
var Node = document.createElement("testing");
Node.appendChild( document.createElement("testingOne") );
var a = document.createAttribute("my_attrib");
node.setAttributeNode(a);
Node.appendChild( document.createElement("TestingTwo") );
Node.appendChild( document.createElement("TestingThree") );
XML.appendChild(Node);
alert(XML.innerHTML);
如何在 Firefox 附加组件中使用 JavaScript 创建 XML 就像上面的示例一样?
你也可以使用 DOMParser,你可能更喜欢这个:
您想要在 Firefox 附加组件中使用 JavaScript 创建一个 XML DOM 树。在 Firefox 附加组件中这样做比在网页中 运行 形式 JavaScript 更复杂。这样做的原因是 window
和 document
变量不能保证被设置。即使已设置,它们也可能未设置为您期望的样子。 Firefox 附加组件处理多个 windows 和多个文档。因此,如果您使用 window
和 document
变量,您应该始终确保将它们设置为您想要的。
在这种情况下,我们只找到最近使用的 Firefox window 的主要 <window>
元素和与当前显示的选项卡关联的 <document>
元素。
以下代码将生成您想要的 XML:
function createDesiredXMLElements() {
//To create elements, we need a <document>. To find a <document> we need a <window>.
//This gets the currently active Firefox XUL window.
// Add/remove a "/" to comment/un-comment the code appropriate for your add-on type.
//* Add-on SDK:
let activeWindow = require('sdk/window/utils').getMostRecentBrowserWindow();
//*/
/* Overlay and bootstrap (from almost any context/scope):
Components.utils.import("resource://gre/modules/Services.jsm");//Services
let activeWindow=Services.wm.getMostRecentWindow("navigator:browser");
//*/
//Get the HTML document for the currently displayed tab in the current Firefox window.
let contentDoc = activeWindow.content.document;
//To create XML elements, we need an XML document. We could do it without creating
// an XML document. But, then we would need to use createElementNS() to specify
// the XML namespace for each element. This way, they inherit the xmlDoc's namespace.
let xmlNS = "http://www.w3.org/XML/1998/namespace";
let xmlDoc = contentDoc.implementation.createDocument(xmlNS,"document");
//Create the XML elements/nodes/attribute and add text.
let testing = xmlDoc.createElement("testing");
let testingOne = xmlDoc.createElement("testingOne");
testingOne.setAttribute("attr","my_attrib");
let testingTwo = xmlDoc.createElement("TestingTwo");
testingTwo.textContent = "abc";
let testingThree = xmlDoc.createElement("TestingThree");
testingThree.textContent = "bbc";
//Place the elements as children of each appropriate node.
testing.appendChild(testingOne);
testingOne.appendChild(testingTwo);
testingOne.appendChild(testingThree);
//Show the alert. Note that the alert function is a method of a <window>:
activeWindow.alert(testing.outerHTML);
}
此代码生成的警报如下所示: