使用Flash builder 4.6创建Flex手机项目读取XML文件方法

以下代码经过平板电脑实际测试,转载请注明出处

在这里,共分为两种读取方法,第一种是直接读取XML文件的节点内容,第二种是以数组形式读取XML文件节点。

工具:Flash builder 4.6

新建 Flex手机项目,这里支持android和ios,选择“基于视图的应用程序”

第一种读取方法:

1. 在src文件夹下创建khlx_lyswzs.xml文件

<?xml version="1.0" encoding="utf-8"?>
<config>
    <xt>
        <question>斩钉()铁</question>  
        <a>A.接</a>
        <b>B.截</b>
        <c>C.结</c>
        <d>D.解</d>
    </xt>
</config> 

2. 在views包中新建“MXML组件”----model_xml.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark" 
        creationComplete="init()"
        title="model_xml">
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        <fx:Model source="khlx_lyswzs.xml" id="model">
            
        </fx:Model>
    </fx:Declarations>
    
    <fx:Script>
        <![CDATA[
            private function init():void{   
                textShow.text = model.xt.question;
            }   
        ]]>
    </fx:Script>
    
    
    <s:TextInput id="textShow">
        
    </s:TextInput>
</s:View>

3. 在平板电脑上测试,将项目的启动页firstView改成firstView="views.model_xml",启动项目,即可读取XML文件的question节点

 

第二种读取方法:以数组形式返回XML文件内容

1. 创建XML文件 khlx_lyswzss.xml

<?xml version="1.0" encoding="utf-8"?>
<questions>
    <xt>
        <question>狼牙山五壮士的场景包括哪些?</question>  
        <itemA>A.顶峰歼敌</itemA>
        <itemB>B.诱敌上山</itemB>
        <itemC>C.炸毁碉堡</itemC>
        <itemD>D.跳下悬崖</itemD>
    </xt>
    <xt>
        <question>斩钉()铁</question>  
        <itemA>A.接</itemA>
        <itemB>B.截</itemB>
        <itemC>C.结</itemC>
        <itemD>D.解</itemD>
    </xt>
</questions>

2. 在views包中新建“MXML组件”----http_xml.mxml

 

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009" 
        xmlns:s="library://ns.adobe.com/flex/spark"
        initialize="HttpXml.send()"
        title="http_xml">
    <fx:Declarations>
        <!-- 将非可视元素(例如服务、值对象)放在此处 -->
        <s:HTTPService url="khlx_lyswzss.xml" id="HttpXml" result="resultHandler(event)">
            
        </s:HTTPService>
    </fx:Declarations>
    
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
            import mx.rpc.events.ResultEvent;
            [Bindable]
            private var myData:ArrayCollection;
            
            private function resultHandler(event:ResultEvent):void{
                myData = event.result.questions.xt;
                
            }
        ]]>
    </fx:Script>
    
    <s:Label id="questionFir" text="{myData.getItemAt(0).question}" x="100" y="10">
        
    </s:Label>
    
    
    
    
    <s:Label id="itemAFir" text="{myData.getItemAt(0).itemA}" x="100" y="110">
        
    </s:Label>
    
    <s:Label id="itemBFir" text="{myData.getItemAt(0).itemB}" x="100" y="210">
        
    </s:Label>
    
    <s:Label id="itemCFir" text="{myData.getItemAt(0).itemC}" x="100" y="310">
        
    </s:Label>
    
    <s:Label id="itemDFir" text="{myData.getItemAt(0).itemD}" x="100" y="410">
        
    </s:Label>
    
    <s:Label id="questionSec" text="{myData.getItemAt(1).question}" x="500" y="10">
        
    </s:Label>
    
    <s:Label id="itemASec" text="{myData.getItemAt(1).itemA}" x="500" y="110">
        
    </s:Label>
    
    <s:Label id="itemBSec" text="{myData.getItemAt(1).itemB}" x="500" y="210">
        
    </s:Label>
    
    <s:Label id="itemCSec" text="{myData.getItemAt(1).itemC}" x="500" y="310">
        
    </s:Label>
    
    <s:Label id="itemDSec" text="{myData.getItemAt(1).itemD}" x="500" y="410">
        
    </s:Label>
    
</s:View>

 

3. 测试同第一种方法。

 

posted @ 2012-06-29 16:03  东师理想--周枫  阅读(982)  评论(0编辑  收藏  举报