ホーム HTML CSS XML JAVASCRIPT   PHP SQL MORE...   リファレンス 事例集    

Why E4X?

« 前章へ 次章へ »

E4Xは、XMLの使用を非常に簡単にします。


E4Xは非常に簡単です

XMLの構文解析や処理に、JavaScriptを使用したことがあれば、E4Xの方が使用するのにずっと簡単であることに気付くでしょう。

E4Xがなければ、XMLを取扱うには、XMLライブラリ(または、XMLコンポーネント)を使用する必要があります。

このライブラリまたはコンポーネントは、ブラウザによって、構文や動作が異ることがあります。


E4Xを使用しない場合

以下の例は、XMLパーサに既存のXMLドキュメント("note.xml")をロードし、 noteからメッセージを表示するクロス・ブラウザ例です:

Example

var xmlDoc;
//code for Internet Explorer
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
displaymessage();
}
// code for Mozilla, Firefox, etc.
else (document.implementation && document.implementation.createDocument)
{
xmlDoc= document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=displaymessage;
}

function displaymessage()
{
document.write(xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue);
}

Try it yourself »

E4Xを使用した場合

次の例は、上と同じですが E4X を使用しています:

var xmlDoc=new XML();
xmlDoc.load("note.xml");
document.write(xmlDoc.body);

ずっと簡単ですね?


« 前章へ 次章へ »