AS3.0中遍历XML中名字含有破折号的节点和对象

本例子将展示这样遍历xml中名字含有破折号的节点如<font-family/>以及使用大括号的[]的含有破折号对象。
'; } else { whichEl.style.display = 'none'; sb.innerHTML=' 显示代码'; whichEl.witdh="300px"; } }   隐藏代码

DE<<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/08/28/parsing-xml-nodes-and-objects-with-dashes-in-their-names-in-actionscript-30/ -->
<mx:Application name="XML_parser_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.rpc.xml.SimpleXMLDecoder;
            import mx.utils.ObjectUtil;

            private function init():void {
                var obj:Object = xmlToObject(xmlDP);

                txtXML.text = xmlDP.toXMLString();
                txtObject.text = ObjectUtil.toString(obj);

                lblXML.text = xmlDP.entry.child('font-family').text();
                lblObject.text = String(obj.root.entry["font-family"]);
            }

            private function xmlToObject(value:XML):Object {
                var xmlStr:String = value.toXMLString();
                var xmlDoc:XMLDocument = new XMLDocument(xmlStr);
                var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
                var resultObj:Object = decoder.decodeXML(xmlDoc);
                return resultObj;
            }
        ]]>
    </mx:Script>

    <mx:XML id="xmlDP">
        <root>
            <entry>
                <font-family>Arial</font-family>
                <font-size>12</font-size>
                <font-weight>normal</font-weight>
                <text-decoration>underline</text-decoration>
                <text>Hello world!</text>
            </entry>
        </root>
    </mx:XML>

    <mx:Panel id="xmlPanel">
        <mx:Text id="txtXML" />
        <mx:ControlBar>
            <mx:Label text="font-family:" />
            <mx:Label id="lblXML" />
        </mx:ControlBar>
    </mx:Panel>

    <mx:Panel id="objPanel" height="{xmlPanel.height}">
        <mx:Text id="txtObject" />
        <mx:ControlBar>
            <mx:Label text="font-family:" />
            <mx:Label id="lblObject" />
        </mx:ControlBar>
    </mx:Panel>

</mx:Application>
DE<

查看Demo演示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值