2012-07-25 91 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 @

相關問題