【问题标题】:The continuing saga of "XML - targeting node attribute, push into a Flash AS3 array"“XML - 定位节点属性,推入 Flash AS3 数组”的继续传奇
【发布时间】:2012-04-04 02:17:31
【问题描述】:

我还有另一个问题源于这个先前提出的问题: XML - targeting node attribute, push into a Flash AS3 array。我被告知要问一个新问题而不是更新旧问题。

这是我的 XML 文件的摘录。 (格式正确,有根节点等,但是太长了,不能全部贴出来。下面只是我关心的部分。

<question id='Q1' uId='99036'  no_ans='2' txt='In a flat structure employees are not expected to provide their bosses with their opinions.' feedback='' type='MC' passingWeight='1' url='media/'>
    <answer id='Q1A1' uId='311288' txt='True' weight='0'/>
    <answer id='Q1A2' uId='311289' txt='False' weight='1'/>
</question>
<question id='Q2' uId='99037'  no_ans='2' txt='In a hierarchy, information typically flows downward.' feedback='' type='MC' passingWeight='1' url='media/'>
    <answer id='Q2A1' uId='311290' txt='True' weight='1'/>
    <answer id='Q2A2' uId='311291' txt='False' weight='0'/>
</question>
<question id='Q3' uId='99038'  no_ans='2' txt='Someone who keeps many projects going at one time is an example of someone who is flexible-time oriented.' feedback='' type='MC' passingWeight='1' url='media/'>
    <answer id='Q3A1' uId='311292' txt='True' weight='1'/>
    <answer id='Q3A2' uId='311293' txt='False' weight='0'/>
</question>

这是我用来从 question 标签中获取 txt 属性的方法。

//load the xml
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("html/BlahBlah/manifest.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
    myXML = new XML(e.target.data);
    trace(myXML.*);



    //.....Your 'myXML' is here....
    questions = {};
    //Extracting question from xml
    for each (var item:XML in myXML.question) {
        questions[item. @ id] = item. @ txt;
    }

}

下面是一个单独的框架上的函数,它扩展了fla的长度。

//Question list
var questions:Object;
//Some method for fetching question from question list
function getQuestionAt( index:Number ):String {
    if (questions["Q" + index] == undefined) {
        throw new Error("Wrong index for question!!!");
    }
    return questions["Q"+index];
}

引用该函数,然后我使用它来定位特定的 txt 属性以填充动态文本字段:

question1_mc.question_txt.htmlText = "<b>Question 1: </b><br>"+ getQuestionAt(1);

我现在需要的是一种从答案标签中获取 txt 属性值并能够从 fla 中的任何地方访问它们的方法。请记住,每个问题都有两个答案。即:

真' weight='0'/ >
False' weight='1'/ >

【问题讨论】:

    标签: xml actionscript-3 flash


    【解决方案1】:

    我认为有一种更简单的方法可以做到这一点。无需加载 XML 并将您的问题存储在对象中,只需引用 XML 对象。 XML 已经是一个对象,因此没有理由将其解析为另一个对象。此外,您将能够根据需要提取所需的所有信息。以下是我的建议:

    var myXML:XML;
    
    var myLoader:URLLoader = new URLLoader();
    myLoader.addEventListener(Event.COMPLETE, complete);
    myLoader.load(new URLRequest("html/BlahBlah/manifest.xml"));
    
    function complete(e:Event):void {
        myLoader.removeEventListener(Event.COMPLETE, complete);
        myXML = new XML(e.target.data);
    
        //Data for question 1
        trace(getQuestionByIndex(1).@txt); // Question
        for each (var o:Object in getAnswersByQuestionIndex(1)) {
            trace(o.@txt); // Answers
        }
    }
    
    function getQuestionByIndex(index:int):XMLList {
        return myXML.question.(@id=="Q"+index);
    }
    
    function getAnswersByQuestionIndex(index:int):XMLList {
        return getQuestionByIndex(index).answer;
    }
    

    【讨论】:

    • 跟踪输出在第一帧上运行良好。我将如何在第 6 帧上填充 3 个文本字段,如下所示? question1_txt.htmlText = ? answer1_txt.htmlText = ? answer2_txt.htmlText = ?
    • 如果你在第一帧定义你的函数并在你进行之前加载你的xml,那么你应该能够调用相同的函数。 question1_txt.htmlText = getQuestionByIndex(1).@txt 和 answer1_txt.htmlText = getAnswersByQuestionIndex(1)[0].@txt
    猜你喜欢
    • 2012-04-01
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    相关资源
    最近更新 更多