【问题标题】:Create XML with element and attribute in Firefox add-on在 Firefox 附加组件中使用元素和属性创建 XML
【发布时间】:2015-09-14 08:09:50
【问题描述】:

我正在尝试在 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?

【问题讨论】:

  • 您提供的示例“XML 格式”似乎与您使用的代码无关(例如,testingtestingOneTestingTwoTestingThree、@ 987654328@ 以“XML 格式”存在,subsub1abcbbc 在您的代码中不存在)。请更改其中一个以反映您在做什么,或者您说您想要什么。事实上,我们将不得不付出更多的努力来尝试猜测您真正想要什么,而不是仅仅能够查看代码/示例并说这是错误的地方。
  • 更改了示例以相互匹配...您现在可以帮帮我吗
  • 顺便说一句:如果您希望将您的评论通知特定的人,您需要在评论中包含他们的 ID,并在其前面加上 @。例如,对我来说是@Makyen。如果您将此作为评论中的第一件事,则系统将提供那些已经在该答案/问题上发布 cmets 的人的自动完成建议。您评论的答案/问题的原始海报将始终得到通知。这个meta post 有更多信息。在这种情况下,我将近一年没有看到您的评论。

标签: javascript xml firefox-addon jscript


【解决方案1】:

您希望在 Firefox 附加组件中使用 JavaScript 创建 XML DOM 树。在 Firefox 插件中这样做比在网页中运行的表单 JavaScript 稍微复杂一些。这样做的原因是不能保证设置windowdocument 变量。即使它们已设置,它们也可能未设置为您期望的那样。 Firefox 附加组件处理多个窗口和多个文档。因此,如果您使用 windowdocument 变量,则应始终确保将它们设置为您想要的。

在这种情况下,我们只找到最近使用的 Firefox 窗口的主要 &lt;window&gt; 元素和与其当前显示的选项卡关联的 &lt;document&gt; 元素。

以下代码将生成您想要的 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);
}

此代码生成的警报类似于:

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2013-11-16
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多