Posted By

adrianparr on 12/21/11


Tagged

parse xml text as2 subtitles namespace captions timedtext timed xmlns


Versions (?)

AS3 Parse TimedText (TT) XML Captions File


 / Published in: ActionScript 3
 

TimedText (TT) XML captions files can have namespaces that cause problems when parsing them in AS3. To get around this you can use this code to remove the namespace from the root XML node using Regex. This example uses

  1. package
  2. {
  3. import flash.display.Sprite;
  4. import flash.events.Event;
  5. import flash.net.URLLoader;
  6. import flash.net.URLLoaderDataFormat;
  7. import flash.net.URLRequest;
  8.  
  9. public class Main extends Sprite
  10. {
  11. private var _xmlFilename:String = "captions.xml";
  12. private var _subtitlesXml:XML;
  13.  
  14. public function Main():void
  15. {
  16. if (stage) init();
  17. else addEventListener(Event.ADDED_TO_STAGE, init);
  18. }
  19.  
  20. private function init(e:Event = null):void
  21. {
  22. removeEventListener(Event.ADDED_TO_STAGE, init);
  23. var urlLoader:URLLoader = new URLLoader();
  24. urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
  25. urlLoader.addEventListener(Event.COMPLETE, onSubtitlesXmlLoadComplete);
  26. urlLoader.load(new URLRequest(_xmlFilename));
  27. }
  28.  
  29. private function onSubtitlesXmlLoadComplete(e:Event):void
  30. {
  31. _subtitlesXml = new XML(e.target.data);
  32. trace("ORIGINAL TIMEDTEXT XML:\n" + _subtitlesXml.toString());
  33. var myXmlStr:String = _subtitlesXml.toString();
  34. var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi");
  35. myXmlStr = myXmlStr.replace(xmlnsPattern, "");
  36. myXmlStr = myXmlStr.replace("aaa:lang=\"en\"", "");
  37. _subtitlesXml = new XML(myXmlStr);
  38. trace("MODIFIED TIMEDTEXT XML:\n" + _subtitlesXml.toString());
  39.  
  40. var numCaptions:int = _subtitlesXml.body.div.p.length();
  41. trace("numCaptions: " + numCaptions);
  42. }
  43.  
  44. }
  45. }
  46.  
  47.  
  48. /////////////////////////////////////////////////////////////////////
  49. // captions.xml
  50. /////////////////////////////////////////////////////////////////////
  51. <?xml version="1.0" encoding="UTF-8"?>
  52. <tt xml:lang="en" xmlns="http://www.w3.org/2006/04/ttaf1" xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling">
  53. <head>
  54. <styling>
  55. </styling>
  56. </head>
  57. <body>
  58. <div>
  59. <p begin="00:00:00.440">Caption 1 here</p>
  60. <p begin="00:00:08.799">Caption 2 here</p>
  61. </div>
  62. </body>
  63. </tt>
  64.  
  65.  
  66. /////////////////////////////////////////////////////////////////////
  67. // OUTPUT
  68. /////////////////////////////////////////////////////////////////////
  69. ORIGINAL TIMEDTEXT XML:
  70. <tt aaa:lang="en" xmlns="http://www.w3.org/2006/04/ttaf1" xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling" xmlns:aaa="http://www.w3.org/XML/1998/namespace">
  71. <head>
  72. <styling/>
  73. </head>
  74. <body>
  75. <div>
  76. <p begin="00:00:00.440">Caption 1 here</p>
  77. <p begin="00:00:08.799">Caption 2 here</p>
  78. </div>
  79. </body>
  80. </tt>
  81. MODIFIED TIMEDTEXT XML:
  82. <tt>
  83. <head>
  84. <styling/>
  85. </head>
  86. <body>
  87. <div>
  88. <p begin="00:00:00.440">Caption 1 here</p>
  89. <p begin="00:00:08.799">Caption 2 here</p>
  90. </div>
  91. </body>
  92. </tt>
  93. numCaptions: 2

Report this snippet  

You need to login to post a comment.