0

いくつかの命令をレンダリングするために、文字列に操作する必要がある 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 ビットを削除して、文字列にします。

だから私はこれの練習版が欲しいとしましょう、私は最終的に

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 で上記のすべてを実行できるかどうか、または正規表現も使用する必要があるかどうかはわかりません。いくつかの例は素晴らしいでしょう。

4

1 に答える 1

1

おそらく正規表現ほどエレガントではありませんが、E4Xで実行できます。<nl>E4x を使用して "\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 に掲載さ ます

于 2012-07-25T22:10:28.453 に答える