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您可以使用xml .@type 检查实践或真实

于 2012-07-25T22:10:28.453 回答