Code to load XML with dtd in AS3


Output the XML with DTD SYSTEM


By tatactic
A basic example of xml file using dtd and xsl : XML - DTD & XSL Example here
See the chapter about xml xsl dtd and xpath here.

Dates.as


( placed in "package me.pirson.classes.dictionaries" )

package me.pirson.classes.dictionaries {
	import flash.events.EventDispatcher;
	public class Dates extends EventDispatcher{
		
		//	IMPORTS
		import flash.net.URLLoader;
		import flash.net.URLRequest;
		import flash.events.Event;
		import flash.events.DataEvent;
		import flash.display.Stage;
		
		//	PRIVATE STATIC vars
		private static var _string:String;
		private static var xmlDictRequest:URLRequest;
		private static var xmlDictLoader:URLLoader;
		//	PRIVATE STATIC consts
		private static const DATE_DICT_FILE:String = "http://tatactic.be/namespaces/AS3NS/dictionnary/dictionnary.xml";
		// Or http://tatactic.be/namespaces/AS3NS/dictionnary/dictionnaryWdtd.xml
		public function Dates():void {
			// constructor code
			super();
			XML.ignoreWhitespace = true;
			xmlDictRequest = new URLRequest(DATE_DICT_FILE);
			xmlDictLoader = new URLLoader();
			xmlDictLoader.addEventListener(Event.COMPLETE,onDictLoaded,false,0,false);
			return void;
		};
		
		public function load():void{
			xmlDictLoader.load(xmlDictRequest);
			return void;
		};
		
		private function onDictLoaded(e:Event):void{
			_string = e.target.data;
			dispatchEvent(new DataEvent(DataEvent.DATA,false,false,string));
			return void;
		};
		
		public function get string():String{
			return _string;
		};

	}
	
}

FLASH file :


"library_Date_dictionnary_XML.fla"
On frame 1 / Sur l'image 1 (layer / calque "actions").
imports + variables

import me.pirson.classes.dictionaries.Dates;
var dates:Dates = new Dates(); var lang:String = "NL"; var monthIndex:uint = 6; var dico:XML;
dates.addEventListener(DataEvent.DATA,dictionaryLoaded,false,0,false);
// call dictionaryLoaded when XML is loaded. // execute la fonction dictionaryLoaded quand l'XML est chargé. dates.load();
function dictionaryLoaded(e:DataEvent):void{ dico = new XML(e.target.string); trace("Text file from XML DTD validated =\n" + e.target.string) trace("to get the month (" + (monthIndex + 1) + ") in \"" + lang + "\" : use dico[lang].months.month[" + (monthIndex) + "]") trace(dico[lang].months.month[monthIndex]) } /* TO VALIDATE THE XML FILE CHECK HERE : http://www.w3schools.com/xml/xml_validator.asp XML VALID & DTD VALID */

Output :


To get the month (7) in "NL" : use dico[lang].months.month[6] Juli

	Text file from XML DTD validated =
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE dictionnary [
<!ELEMENT dictionnary (FR,NL)>
<!ELEMENT FR (months,days,ChristianCelebrations)>
<!ELEMENT months (month+)>
<!ELEMENT month (#PCDATA)>
<!ELEMENT days (day+)>
<!ELEMENT day (#PCDATA)>
<!ELEMENT ChristianCelebrations (celebration+)>
<!ELEMENT celebration (#PCDATA)>
<!ELEMENT NL (months,days,ChristianCelebrations)>
]>
<dictionnary>
<FR>
<months>
<month>Janvier</month>
<month>Février</month>
<month>Mars</month>
<month>Avril</month>
<month>Mai</month>
<month>Juin</month>
<month>Juillet</month>
<month>Août</month>
<month>Septembre</month>
<month>Octobre</month>
<month>Novembre</month>
<month>Décembre</month>
</months>
<days>
<day>Lundi</day>
<day>Mardi</day>
<day>Mercrcedi</day>
<day>Jeudi</day>
<day>Vendredi</day>
<day>Samedi</day>
<day>Dimanche</day>
</days>
<ChristianCelebrations>
<celebration>Jour de l'an</celebration>
<celebration>Épiphanie</celebration>
<celebration>Mardi Gras</celebration>
<celebration>Rameaux</celebration>
<celebration>Pâques</celebration>
<celebration>Lundi de Pâques</celebration>
<celebration>Ascension</celebration>
<celebration>Pentecôte</celebration>
<celebration>Lundi de Pentecôte</celebration>
<celebration>Assomption</celebration>
<celebration>Toussaint</celebration>
<celebration>Noël</celebration>
<celebration>fête des :</celebration>
</ChristianCelebrations>
</FR>
<NL>
<months>
<month>Januari</month>
<month>Februari</month>
<month>Maart</month>
<month>April</month>
<month>Mei</month>
<month>Juni</month>
<month>Juli</month>
<month>Augustus</month>
<month>September</month>
<month>October</month>
<month>November</month>
<month>December</month>
</months>
<days>
<day>Maandag</day>
<day>Dinsdag</day>
<day>Woensdag</day>
<day>Donderdag</day>
<day>Vrijdag</day>
<day>Zaterdag</day>
<day>Zondag</day>
</days>
<ChristianCelebrations>
<celebration>Nieuwjaardag</celebration>
<celebration>Driekoningen</celebration>
<celebration>Vastenavond</celebration>
<celebration>Palmzondag</celebration>
<celebration>Pasen</celebration>
<celebration>Tweede Paasdag</celebration>
<celebration>Hemelvaart</celebration>
<celebration>Pinksteren</celebration>
<celebration>Tweede Pinksterdag</celebration>
<celebration>Maria-Hemelvaart</celebration>
<celebration>Allerheiligen</celebration>
<celebration>Kerstmis</celebration>
<celebration>'s naamdag</celebration>
</ChristianCelebrations>
</NL>
</dictionnary>
to get the month (7) in "NL" : use dico[lang].months.month[6] Juli

XHTML 1.0 STRICT