From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/nsidomparser/index.html | 56 +++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 files/zh-cn/nsidomparser/index.html (limited to 'files/zh-cn/nsidomparser/index.html') diff --git a/files/zh-cn/nsidomparser/index.html b/files/zh-cn/nsidomparser/index.html new file mode 100644 index 0000000000..6b27366e91 --- /dev/null +++ b/files/zh-cn/nsidomparser/index.html @@ -0,0 +1,56 @@ +--- +title: nsIDOMParser +slug: nsIDOMParser +tags: + - DOMParser + - nsIDOMParser +translation_of: Mozilla/Tech/XPCOM/Reference/Interface/nsIDOMParser +--- +
+ 注意: 如果你是一名 Web 开发者, 请参考 DOMParser 文档。
+

创建 DOMParser

+

To create a DOMParser object from a web page or a chrome script running in a window, simply use new DOMParser(). When you create a DOMParser from a privileged script, you can pass parameters to the constructor, more on that below.

+

To create a DOMParser when the constructor is not available (e.g., from a JS XPCOM component, a JS module, or an xpcshell test), use:

+
var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
+             .createInstance(Components.interfaces.nsIDOMParser);
+// optionally, call parser.init(principal, documentURI, baseURI);
+
+

Principals, document and base URI

+

Note: This section covers changes introduced to DOMParser in Gecko 1.9.

+

(This section is only relevant to Firefox extensions--not to Web content.)

+

To create a document, the parser needs to specify a principal (see Security check basics), a base URI (see document.baseURIObject), and a documentURI.

+

These values are automatically determined as defined below, but if you work with DOMParser from privileged code, you can override the defaults by providing arguments to the DOMParser constructor or calling parser.init(). Usually you don't need to do that. If you come across a situation when these matter, feel free to ask questions in mozilla.dev.tech.dom and update this documentation to mention these cases.

+ +

Cases where these values matter:

+ +

解析字符串

+

Web platform documentation 中所述的,一旦你已创建了一个 DOMParser 对象,你可以使用它的 parseFromString 方法来解析 XML 或 HTML。

+

示例

+

Within the context of a window:

+
var parser = new DOMParser();
+var doc = parser.parseFromString(aStr, "application/xml");
+
+

Outside of a window (e.g., a JS XPCOM component, a JS module, or an xpcshell test):

+
var parser = Components.classes["@mozilla.org/xmlextras/domparser;1"]
+             .createInstance(Components.interfaces.nsIDOMParser);
+var doc = parser.parseFromString(aStr, "application/xml");
+
+

Using Components.Constructor():

+
const DOMParser = new Components.Constructor("@mozilla.org/xmlextras/domparser;1", "nsIDOMParser");
+var parser = new DOMParser();
+parser.init(principal, documentURI, baseURI);
+var doc = parser.parseFromString(aStr, "application/xml");
+
-- cgit v1.2.3-54-g00ecf