コンテンツにスキップ

ECMAScript for XML

出典: フリー百科事典『ウィキペディア(Wikipedia)』

ECMAScript for XMLE4XECMAScriptActionScriptDMDScriptJavaScriptJScript XMLDOMXMLE4XXMLE4XXML

E4XEcmaECMA-357 (PDF) 200462200512

E4X2014Mozilla 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 20022BEA Weblogic Workshop 7.0 BEA Rhino E4X John Schneider  BEA  XML E4X[1]

E4XSpiderMonkeyGeckoJavaScript RhinoMozillaJavaJavaScript

Mozilla Firefox  Gecko E4X 使1.5Firefox 1721[2]Firefox 1.5  type  "; e4x=1" <script type="application/javascript; e4x=1">

 ActionScript3 E4X 2006 Adobe Flex 2.0  Flash Player9Flash CS3Adobe AIRAdobe Acrobat/Reader8.0

Aptana Jaxer Ajax Mozilla 使E4X 

 (CMS)  Alfresco Community Edition 2.9B  E4X 

[]


E4XDOME4X/

[]

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")))

脚注[編集]

関連項目[編集]

外部リンク[編集]