nsIDOMParser 编辑
Creating a 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 toDOMParser
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.
- When a
DOMParser
is instantiated by callingnew DOMParser()
, it inherits the calling code's principal (except that for chrome callers the principal is set to the null principal) and thedocumentURI
andbaseURI
of the window the constructor came from. - If the caller has UniversalXPConnect privileges, it can pass parameters to
new DOMParser()
. If fewer than three parameters are passed, the remaining parameters will default tonull
.- The first parameter is the principal to use; this overrides the default principal normally inherited.
- The second parameter is the
documentURI
to use. - The third parameter is the
baseURI
to use.
- If you instantiate a
DOMParser
by callingcreateInstance()
, and you don't call theDOMParser
'sinit()
method, attempting to initiate a parsing operation will automatically callinit()
on theDOMParser
with a null principal andnull
pointers fordocumentURI
andbaseURI
.
Cases where these values matter:
- If you don't specify the document URI by calling init() after creating the parser via
createInstance()
the created documents will use a moz-nullprincipal:{<guid>} URI, which will show in the Error Console in parsing errors, in particular. - Supposedly, if you want to create objects that some particular set of unprivileged code will be able to access (see discussion in bug 565480).
Parsing a string
Once you've created a DOMParser
object, you can use its parseFromString
method to parse XML or HTML as described in the Web platform documentation.
Examples
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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论