We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS

翻譯不完整。請協助 翻譯此英文文件

這是一個實驗中的功能
此功能在某些瀏覽器尚在開發中,請參考兼容表格以得到不同瀏覽器用的前輟。

DOMParser可以將XML或是HTML格式的字串轉成DOM 文件. DOMParser的規格請參閱DOM解譯與串流化.

請注意XMLHttpRequest解譯的是URL連結內容裡的XML與HTML文件.

產生一個 DOMParser

new DOMParser()" 可產生DOMParser.

關於如何在Firefox外掛程式中產生DOMParser,請參考nsIDOMParser文件

解譯 XML

產生解譯物件後,請呼叫parseFromString方法函式來將XML字串轉換成DOM物件:

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

錯誤處理

請注意如果解譯過程出錯,目前的DOMParser不會丟出異常物件(exception),但是會回傳一個錯誤文件(請看程式臭蟲bug 45566):

<parsererror xmlns="http://www.mozilla.org/newlayout/xml/parsererror.xml">
(error description)
<sourcetext>(a snippet of the source XML)</sourcetext>
</parsererror>

解譯錯誤也會記錄在錯誤終端機中(Error Console), 紀錄裡頭的文件URI (如下) 則為錯誤來源.

解譯 SVG 或 HTML 文件

DOMParser也可以用來解譯 SVG 文件 (Firefox 10.0 / Thunderbird 10.0 / SeaMonkey 2.7) 或是 HTML 文件 (Firefox 12.0 / Thunderbird 12.0 / SeaMonkey 2.9). 可以依 MIME 格式,輸出三種不同格式. 如果MIME 格式是 text/xml,輸出的格式為 XMLDocument, 如果 MIME 格式是 image/svg+xml, 輸出格式為 SVGDocument, 如果 MIME 格式是 text/html, 輸出格式則為 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 外掛程式

/*
 * DOMParser HTML extension
 * 2012-09-04
 * 
 * By Eli Grey, http://eligrey.com
 * Public domain.
 * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
 */

/*! @source 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

瀏覽器相容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
XML support 1 1.0 (1.7 or earlier) 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 (Yes) (Yes) ? (Yes) ?
SVG support ? 10.0 (10.0) ? ? ?
HTML support ? 12.0 (12.0) ? ? ?

參考資料

文件標籤與貢獻者

標籤: 
此頁面的貢獻者: kenttsai
最近更新: kenttsai,