ECMAScript for XML
ECMAScript for XML(E4X)は、ECMAScript(ActionScript、DMDScript、JavaScript、JScript を含む)にネイティブのXMLサポートを追加するプ…
ECMAScript for XML(E4X)は、ECMAScript(ActionScript、DMDScript、JavaScript、JScript を含む)にネイティブのXMLサポートを追加するプログラミング言語拡張である。その目的は、DOMインタフェースの代替として、単純な構文でXML文書にアクセスできるインタフェースを提供することである。E4Xがリリースされるまで、XMLへのアクセスには常にオブジェクトレベルが関与していた。E4XではXMLを文字や整数と同様のプリミティブ型として扱う。そのため、アクセスが高速化され、サポートが容易になり、プログラムの構成要素(データ構造)としても扱いやすくなる。
E4XはEcmaインターナショナルがECMA-357 (PDF) として標準化した。初版は2004年6月に公表され、第2版が2005年12月に公表された。
E4Xは、2014年にMozilla Foundationによって非推奨とされている[1]。
例
var sales = <sales vendor="John">
<item type="peas" price="4" quantity="6"/>
<item type="carrot" price="3" quantity="10"/>
<item type="chips" price="5" quantity="3"/>
</sales>;
alert( sales.item.(@type == "carrot").@quantity );
alert( sales.@vendor );
for each( var price in sales..@price ) {
alert( price );
}
実装
最初の実装は Terry Lucas と John Schneider が設計したもので、2002年2月にリリースされたBEAシステムズの Weblogic Workshop 7.0 に含まれていた。BEAの実装は Rhino に基づくもので、E4X の標準化が完了する以前にリリースされている。リリース時、John Schneider は BEA の XML 拡張について記事を書いた。E4X言語以前のリファレンス文書が現在も公開されている[1]。
E4Xは、SpiderMonkey(GeckoのJavaScriptエンジン)や Rhino(同じくMozilla用にJavaで書かれたJavaScriptエンジン)で実装されている。
Mozilla Firefox は Gecko ベースなので、E4X を使ったスクリプトを実行可能であった(バージョン1.5以降)が、Firefox 17から段階的に無効化され、同21で削除される予定である[2]。なお、Firefox 1.5 で正しくスクリプトを実行するには、スクリプトの type 属性の最後に "{{{1}}}" を追加する必要がある(例えば、{{{1}}})。
アドビの ActionScript 3 でも E4X を完全サポートしている。これが公式にリリースされたのは、2006年の Adobe Flex 2.0 と Flash Player 9 の一部としてである。他に、Flash CS3、Adobe AIR、Adobe Acrobat/Reader(8.0以降)でもサポートされている。
Aptanaの Jaxer Ajax アプリケーションサーバは、Mozilla のエンジンをサーバ側で使っているため、E4X に対応している。
コンテンツ管理システム (CMS) の Alfresco Community Edition 2.9B でも E4X をサポートしている。
批判
多くのE4X実装は、DOMノードとE4Xモデルの間で、インポート/エクスポートする手段を提供していない。
競合規格
JSON
JSONは XML の代替となる可能性がある。JSON は XML に似たオブジェクト指向のデータ記述言語である。JSON は ECMA-404 として ECMAインタナショナル により標準化されており、JavaScript からは、 JSON オブジェクトのメソッドにより操作する。
上掲の例を JSON を使った場合、次のようになる。
const json = `{
"vendor": "John",
"items": [
{ "type": "peas", "price": 4, "quantity": 6 },
{ "type": "carrot", "price": 3, "quantity": 10 },
{ "type": "chips", "price": 5, "quantity": 3 }
]
}`;
const sales = JSON.parse(json);
alert(sales.items.find(item => item.type === "carrot").quantity);
alert(sales.vendor);
sales.items.forEach(item => alert(item.price));
DOMParser
JavaScript には、XML や HTML の文字列から DOM の Document クラスを生成する DOMParser インターフェイスが用意されている。
E4X の例を DOMParser インターフェイスを使った場合、次のようになる。
const xml = `<sales vendor="John">
<item type="peas" price="4" quantity="6"/>
<item type="carrot" price="3" quantity="10"/>
<item type="chips" price="5" quantity="3"/>
</sales>`;
const parser = new DOMParser();
const sales = parser.parseFromString(xml, 'text/xml');
const items = Array.from(sales.getElementsByTagName("item"));
alert(items.find(item => item.getAttribute("type") === "carrot").getAttribute("quantity"));
alert(sales.getElementsByTagName("sales").item(0).getAttribute("vendor"));
items.forEach(item => alert(item.getAttribute("price")))
脚注
関連項目
- JSX (JavaScript) - Metaが開発したJavaScript拡張構文。
外部リンク
- ECMA-357 ECMAScript for XML (E4X) Specification、日本語試訳
- Slides from 2005 E4X Presentation by Brendan Eich, Mozilla Chief Architect
- E4X at Mozilla Developer Center。日本語版
- Introducing E4X at xml.com、E4X と JSON の比較
- Processing XML with E4X at Mozilla Developer Center
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.