我可以使用e4x还是我还需要正则表达式?

时间:2012-07-25 02:02:03

标签: actionscript-3 flex actionscript e4x

我有一些XML需要被操作成一个字符串来呈现一些指令。文字看起来像这样

<?xml version="1.0" encoding="UTF-8"?>
<instructions id="detection" version="1.0">
<instruction task="detection">
    <phrase type="header">HAS THE CARD TURNED OVER?<nl/><nl/><nl/></phrase>
    <phrase type="practice">you are now going to do a practice.<nl/><nl/></phrase>
    <phrase type="real">You are now going to do a test.<nl/><nl/></phrase>
    <phrase>As soon as the card turns face up:<nl/><nl/></phrase>
    <phrase><ts/><ts/>Press YES.<nl/><nl/></phrase>
    <phrase>Go as fast as you can and try not to make any mistakes.<nl/><nl/></phrase>
    <phrase>If you press YES before a card turns face up, you will hear an error sound.</phrase>
</instruction>
</instructions>

现在,我需要做的就是以下

  1. 将所有<nl/>替换为\ n
  2. 将所有<ts/>替换为\ t
  3. 有条件地选择练习或真实,可能是删除其他
  4. 删除剩余的所有XML位以产生字符串。
  5. 所以我想说我想要练习这个版本,我最终应该

    HAS THE CARD TURNED OVER?\n\n\n
    you are now going to do a practice.\n\n
    As soon as the card turns face up:\n\n
    \t\tPress YES.\n\n
    Go as fast as you can and try not to make any mistakes.\n\n
    If you press YES before a card turns face up, you will hear an error sound.
    

    现在,我有机会改变XML的结构,如果当前的形式不适合这个,但我不确定是否可以使用e4X完成上述所有操作,或者我还需要使用正则表达式?一些例子会很棒。

1 个答案:

答案 0 :(得分:1)

可以用E4X完成,可能不如正则表达式那么优雅。 以下是使用E4x将<nl>替换为“\ n”的示例:

package
{
    import flash.display.Sprite;

    public class e4xStuff extends Sprite
    {
        private var srcxml:XML;

        public function e4xStuff()
        {
            srcxml = new XML(   '<instructions id="detection" version="1.0">' +
                '<instruction task="detection">' +
                '<phrase type="header">HAS THE CARD TURNED OVER?<nl/><nl/><nl/></phrase>' +
                '<phrase type="practice">you are now going to do a practice.<nl/><nl/></phrase>' +
                '<phrase type="real">You are now going to do a test.<nl/><nl/></phrase>' +
                '<phrase>As soon as the card turns face up:<nl/><nl/></phrase>' +
                '<phrase><ts/><ts/>Press YES.<nl/><nl/></phrase>' +
                '<phrase>Go as fast as you can and try not to make any mistakes.<nl/><nl/></phrase>' +
                '<phrase>If you press YES before a card turns face up, you will hear an error sound.</phrase>' +
                '</instruction>' +
                '</instructions>');


            processNode(srcxml);
            trace(srcxml);
        }

        private function processNode(xml:XML):XML
        {
            //replace <nl/> with \n
            if(xml.name() == "nl")
            {
                return new XML("\n");
            }

            var children:XMLList = xml.children();
            if(children.length() == 0)
            {
                return xml;
            }

            //remove the children
            xml.setChildren(new XMLList());   

            //put the children back, one-by-one, after checking for <nl/>
            for(var i:int=0; i<children.length(); i++)
            {
                xml.appendChild(processNode(children[i])); 
            }
            return xml;
        }
    }
}

E4X方法列表发布在http://wso2.org/project/mashup/0.2/docs/e4xquickstart.html 您可以使用 xml 。@ type

检查练习或真实
相关问题