2012-07-25 93 views
0

我有一些XML需要被处理成一个字符串来呈现一些指令。文本看起来像这样我可以只使用e4x还是我也需要regex?

<?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完成上述所有操作,或者我还需要使用正则表达式的?一些例子会很棒。

+0

进行类型。你实际上需要“\ n”,还是会出现一个视觉上的突破,比如你使用
标签的工作? – 2012-07-25 02:50:33

+0

它进入一个textarea,它目前使用\ n \ t硬编码文本,我们将它移动到一个xml配置系统 – 2012-07-25 02:59:31

+0

我会使用
而不是试图用\ n替换。我想你可以在需要标签的地方使用标签。此时,您只需在包含您的实际值或练习值的XMLList上调用toString()。如果您未在TextArea上设置condenseWhite,则可能甚至不需要
标记,但可以通过将回车位置放在需要的位置来格式化文本。 – 2012-07-25 03:16:29

回答

1

它可以用E4X完成,可能不像正则表达式那么优雅。 这里是为“\ n”使用E4X更换<nl>的例子:

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 @

相关问题