2014-12-09

ActionScript3 XML element

http://stackoverflow.com/questions/15977179/determine-if-xml-node-attribute-is-empty-in-as3

<item elem=''>

elem属性はあるけどカラ文字列 who属性は存在しない

item.@elem
item.@who

trace()しても同じ

length()で区別がつく

var xml:XML = 
<data>
  <item id='1' elem=''>
    <name>あんぱん</name>
    <price>80</price>
  </item>
  <item id='2'>
    <name>メロンパン</name>
    <price>100</price>
  </item>
  <item id='3'>
    <name>カレーパン</name>
    <price>120</price>
  </item>
</data>;

var xmlList:XMLList = xml.item;

trace('xmlList[0].@id=' + xmlList[0].@id);
trace('xmlList[0].name=' + xmlList[0].name);


if (xmlList[0].@elem) trace('xmlList[0].@elem has some.');
if (xmlList[0].@elem === null) trace('xmlList[0].@elem=null');
if (xmlList[0].@elem === '') trace('xmlList[0].@elem=""');
if (xmlList[0].@elem.toString() === null) trace('xmlList[0].@elem.toString()=null');
if (xmlList[0].@elem.toString() === '') trace('xmlList[0].@elem.toString()=""');
trace('xmlList[0].@elem=' + xmlList[0].@elem);
trace('xmlList[0].@elem.length()=' + xmlList[0].@elem.length());

if (xmlList[0].@who) trace('xmlList[0].@who has some.');
if (xmlList[0].@who === null) trace('xmlList[0].@who=null');
if (xmlList[0].@who === '') trace('xmlList[0].@who=""');
if (xmlList[0].@who.toString() === null) trace('xmlList[0].@who.toString()=null');
if (xmlList[0].@who.toString() === '') trace('xmlList[0].@who.toString()=""');
trace('xmlList[0].@who=' + xmlList[0].@who);
trace('xmlList[0].@who.length()=' + xmlList[0].@who.length());

結果

xmlList[0].@id=1
xmlList[0].name=あんぱん
xmlList[0].@elem has some.
xmlList[0].@elem.toString()=""
xmlList[0].@elem=
xmlList[0].@elem.length()=1
xmlList[0].@who has some.
xmlList[0].@who.toString()=""
xmlList[0].@who=
xmlList[0].@who.length()=0

参考 http://itpro.nikkeibp.co.jp/article/COLUMN/20080507/300850/?ST=develop&P=3

0 件のコメント:

コメントを投稿

人気記事