Revision: 54351
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at December 21, 2011 00:10 by adrianparr
Initial Code
package { import flash.display.Sprite; import flash.events.Event; import flash.net.URLLoader; import flash.net.URLLoaderDataFormat; import flash.net.URLRequest; public class Main extends Sprite { private var _xmlFilename:String = "captions.xml"; private var _subtitlesXml:XML; public function Main():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); var urlLoader:URLLoader = new URLLoader(); urlLoader.dataFormat = URLLoaderDataFormat.TEXT; urlLoader.addEventListener(Event.COMPLETE, onSubtitlesXmlLoadComplete); urlLoader.load(new URLRequest(_xmlFilename)); } private function onSubtitlesXmlLoadComplete(e:Event):void { _subtitlesXml = new XML(e.target.data); trace("ORIGINAL TIMEDTEXT XML:\n" + _subtitlesXml.toString()); var myXmlStr:String = _subtitlesXml.toString(); var xmlnsPattern:RegExp = new RegExp("xmlns[^\"]*\"[^\"]*\"", "gi"); myXmlStr = myXmlStr.replace(xmlnsPattern, ""); myXmlStr = myXmlStr.replace("aaa:lang=\"en\"", ""); _subtitlesXml = new XML(myXmlStr); trace("MODIFIED TIMEDTEXT XML:\n" + _subtitlesXml.toString()); var numCaptions:int = _subtitlesXml.body.div.p.length(); trace("numCaptions: " + numCaptions); } } } ///////////////////////////////////////////////////////////////////// // captions.xml ///////////////////////////////////////////////////////////////////// <?xml version="1.0" encoding="UTF-8"?> <tt xml:lang="en" xmlns="http://www.w3.org/2006/04/ttaf1" xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling"> <head> <styling> </styling> </head> <body> <div> <p begin="00:00:00.440">Caption 1 here</p> <p begin="00:00:08.799">Caption 2 here</p> </div> </body> </tt> ///////////////////////////////////////////////////////////////////// // OUTPUT ///////////////////////////////////////////////////////////////////// ORIGINAL TIMEDTEXT XML: <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"> <head> <styling/> </head> <body> <div> <p begin="00:00:00.440">Caption 1 here</p> <p begin="00:00:08.799">Caption 2 here</p> </div> </body> </tt> MODIFIED TIMEDTEXT XML: <tt> <head> <styling/> </head> <body> <div> <p begin="00:00:00.440">Caption 1 here</p> <p begin="00:00:08.799">Caption 2 here</p> </div> </body> </tt> numCaptions: 2
Initial URL
Initial Description
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
Initial Title
AS3 Parse TimedText (TT) XML Captions File
Initial Tags
xml, text
Initial Language
ActionScript 3