Diese Übersetzung ist unvollständig. Bitte helfen Sie, diesen Artikel aus dem Englischen zu übersetzen.
Dies ist eine experimentelle Technologie
Da diese Technologie noch nicht definitiv implementiert wurde, sollte die Browser Kompatibilität beachtet werden. Es ist auch möglich, dass der Syntax in einer späteren Spezifikation noch geändert wird.
Der DOMParser kann XML oder HTML aus einem string in ein DOM Document parsen. Der DOMParser ist spezifiziert in DOM Parsing and Serialization.
XMLHttpRequest unterstützt das parsen von XML und HTML Dokumenten auf die über eine URL zugegriffen wird.
DOMParser Erzeugen
Um einen neuen DOMParser zu erzeugen benutze einfach new DOMParser().
Für mehr Informationen über das erstellen von einem DOMParser in einer Firefox Erweiterung, benutze die nsIDOMParser Dokumentation
XML parsen
Nachdem du einmal ein Parser-Objekt erzeugt hast, kannst du XML Strings mit der parseFromString Methode parsen.
var parser = new DOMParser(); var doc = parser.parseFromString(stringContainingXMLSource, "application/xml");
Fehler Behandlung
Note that if the parsing process failed, DOMParser currently does not throw an exception, but instead returns an error document (see Bug 45566):
<parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml"> (error description) <sourcetext>(a snippet of the source XML)</sourcetext> </parsererror>
The parsing errors are also reported to the Error Console, with the document URI (see below) as the source of the error.
Parsing an SVG or HTML document
The DOMParser can also be used to parse a SVG document (Firefox 10.0 / Thunderbird 10.0 / SeaMonkey 2.7) or a HTML document (Firefox 12.0 / Thunderbird 12.0 / SeaMonkey 2.9). There are three different results possible, selected by the MIME type given. If the MIME type is text/xml, the resulting object will be an XMLDocument, if the MIME type is image/svg+xml, it will be an SVGDocument and if the MIME type is text/html, it will be an HTMLDocument.
var parser = new DOMParser(); var doc = parser.parseFromString(stringContainingXMLSource, "application/xml"); // returns a Document, but not a SVGDocument nor a HTMLDocument parser = new DOMParser(); doc = parser.parseFromString(stringContainingXMLSource, "image/svg+xml"); // returns a SVGDocument, which also is a Document. parser = new DOMParser(); doc = parser.parseFromString(stringContainingHTMLSource, "text/html"); // returns a HTMLDocument, which also is a Document.
DOMParser HTML extension for other browsers
/* inspired by https://gist.github.com/1129031 */
/*global document, DOMParser*/
(function(DOMParser) {
"use strict";
var
proto = DOMParser.prototype
, nativeParse = proto.parseFromString
;
// Firefox/Opera/IE throw errors on unsupported types
try {
// WebKit returns null on unsupported types
if ((new DOMParser()).parseFromString("", "text/html")) {
// text/html parsing is natively supported
return;
}
} catch (ex) {}
proto.parseFromString = function(markup, type) {
if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {
var
doc = document.implementation.createHTMLDocument("")
;
if (markup.toLowerCase().indexOf('<!doctype') > -1) {
doc.documentElement.innerHTML = markup;
}
else {
doc.body.innerHTML = markup;
}
return doc;
} else {
return nativeParse.apply(this, arguments);
}
};
}(DOMParser));
DOMParser from Chrome/JSM/XPCOM/Privileged Scope
See article here: nsIDOMParser
Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| XML support | 1 | 1.0 (1.7 oder früher) | 9 | 8 | 3.2 |
| SVG support | 4 | 10.0 (10.0) | 10 | 15 | 3.2 |
| HTML support | 30 | 12.0 (12.0) | 10 | 17 | 7.1 |
| Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|
| XML support | (Ja) | (Ja) | ? | (Ja) | ? |
| SVG support | ? | 10.0 (10.0) | ? | ? | ? |
| HTML support | ? | 12.0 (12.0) | ? | ? | ? |