1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
---
title: E4X
slug: Archive/Web/E4X
translation_of: Archive/Web/E4X
---
<div>
<div class="overheadIndicator obsolete obsoleteHeader"><p><strong><span title="This is an obsolete API and is no longer guaranteed to work."><i class="icon-trash"> </i></span> 已废弃</strong><br>This feature is obsolete. Although it may still work in some browsers, its use is discouraged since it could be removed at any time. Try to avoid using it.</p></div></div>
<div>
</div>
<div>
<div class="warning warningHeader">
<p><strong>Warning:</strong> E4X已经过时.从Firefox 17开始,E4X在普通网页中被默认禁用,从Firefox 20开始,E4X在浏览器界面中被默认禁用,从Firefox 21开始,E4X被完全删除.使用DOMParser/DOMSerializer或者其他算法来代替.</p>
</div></div>
<p><strong>ECMAScript for XML</strong> (<strong>E4X</strong>) is a programming language extension that adds native XML support to <a href="/zh-CN/JavaScript" title="zh-CN/JavaScript">JavaScript</a>. It does this by providing access to the XML document in a form that feels natural for ECMAScript programmers. The goal is to provide an alternative, simpler syntax for accessing XML documents than via <a href="/zh-CN/DOM" title="zh-CN/DOM">DOM</a> interfaces. A valid alternative to E4X is a non-native <a href="/zh-CN/JXON" title="zh-CN/JXON">JXON algorithm</a>.</p>
<p>E4X is standardized by Ecma International in <a class="external" href="http://www.ecma-international.org/publications/standards/Ecma-357.htm">ECMA-357 standard</a> (currently in its second edition, December 2005).</p>
<p>E4X is implemented (at least partially) in <a href="/zh-CN/SpiderMonkey" title="zh-CN/SpiderMonkey">SpiderMonkey</a> (<a href="/zh-CN/Gecko" title="zh-CN/Gecko">Gecko</a>'s JavaScript engine) and in <a href="/zh-CN/Rhino" title="zh-CN/Rhino">Rhino</a> (JavaScript engine written in Java).</p>
<p> </p>
<div class="note">
<strong>Note:</strong> In Gecko 1.8 based browsers such as Firefox 1.5, E4X is already partially enabled for web page authors. To fully enable E4X, the <code><script></code> element needs to have the MIME type "text/javascript;e4x=1" (i.e. have an attribute of the form <code>type="text/javascript;e4x=1"</code>). The difference between the two modes is that without the "e4x=1" MIME type, any statement-level XML/HTML comment literals (<code><span class="nowiki"><!--...--></span></code>) are ignored for backwards compatibility with the comment hiding trick, and CDATA sections (<code><![CDATA[...]]></code>) are not parsed as CDATA literals (which leads to a JS syntax error in HTML since HTML's <code><script></code> element produces an implicit CDATA section, and therefore cannot contain explicit CDATA sections).
<p><span class="comment">someone verify the above</span></p>
</div>
<h3 id="Known_bugs_and_limitations" name="Known_bugs_and_limitations">已经的bug和限制</h3>
<ul>
<li>It is not currently possible to access a DOM object through E4X (<a href="https://bugzilla.mozilla.org/show_bug.cgi?id=270553" title="E4x<->DOM">bug 270553</a>)</li>
<li>E4X doesn't support parsing XML declaration (</li>
<li><?xml version=...?>) (see <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=336551" title='E4X: Implement ability to process new XML("&amp;lt;?xml...?> ...")'>bug 336551</a>). You may get SyntaxError "xml is a reserved identifier" (despite the XML being in a string).</li>
</ul>
<p>Workaround:</p>
<pre class="brush: js">var response = xmlhttprequest.responseText; // bug 270553
response = response.replace(/^<\?xml\s+version\s*=\s*(["'])[^\1]+\1[^?]*\?>/, ""); // bug 336551
var e4x = new XML(response);
</pre>
<h3 id="Resources" name="Resources">资源</h3>
<ul>
<li><a href="/zh-CN/E4X_Tutorial" title="zh-CN/E4X_Tutorial">E4X Tutorial</a> <a class="internal" href="/zh-CN/E4X/Processing_XML_with_E4X" title="zh-CN/E4X/Processing XML with E4X">Processing XML with E4X</a> on MDN.</li>
<li><a href="/zh-CN/E4X_for_templating" title="zh-CN/E4X for templating">E4X for templating</a></li>
<li>See the list of<a href="/Special:Tags?tag=E4X&language=en" title="Special:Tags?tag=E4X&language=en"> E4X-related pages</a> on MDN:</li>
<li><a class="external" href="http://www.ecma-international.org/publications/standards/Ecma-357.htm">ECMA-357 standard</a></li>
<li><a class="external" href="/presentations/xtech2005/e4x" title="presentations/xtech2005/e4x">Brendan's presentation</a></li>
<li><a class="external" href="http://web.archive.org/web/20080703182907/http://www.faqts.com/knowledge_base/index.phtml/fid/1762" title="http://web.archive.org/web/20080703182907/http://www.faqts.com/knowledge_base/index.phtml/fid/1762">E4X at faqts.com</a></li>
<li><a class="external" href="http://rephrase.net/days/07/06/e4x">E4X quick reference at rephrase.net</a></li>
</ul>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a class="internal" href="/zh-CN/JXON" title="zh-CN/JXON">JXON</a></li>
<li><a class="internal" href="/zh-CN/XPath" title="zh-CN/XPath">XPath</a></li>
<li><a class="internal" href="/zh-CN/XML" title="zh-CN/XML">XML</a></li>
<li><a class="internal" href="/zh-CN/Parsing_and_serializing_XML" title="zh-CN/Parsing_and_serializing_XML">XML的解析和序列化</a></li>
</ul>
|