Skip to main content
Inspiring
June 9, 2010
Answered

Applying CSS to XML data in AS3

  • June 9, 2010
  • 1 reply
  • 1834 views

I realize this topic has been repeated several times, but despite having reviewed many discussions and web pages on the subject I've not been able to figure out why mine isn't working, so I'm looking for help.

Here is my relevant code:

CSS:

@charset "utf-8";

/* CSS Document */

p {

font-family:Arial;

font-size:25px;

color:#000;

}

XML:
<?xml version="1.0" encoding="utf-8"?>
<tbContent>
<slide> <!-- 0 -->
<title>Table of Contents</title>
<type>content</type>
<content><![CDATA[<p>What is a Mantoux Skin Test?</p>
<p>How to Administer a Mantoux Skin Test</p>
<p>How to Read a Mantoux Skin Test</p>
<p>How to Interpret the Reading</p>]]></content>
<image>Blank.jpg</image>
<popup>0</popup>
<footnote>0</footnote>
</slide>
</tbContent>
AS3:
var container:content_mc = new content_mc();
// Set up the xml file objects
var xmlData:XML = new XML();
XML.ignoreWhitespace = true;
var xmlLoader:URLLoader = new URLLoader();
// CSS external document
var cssLoader:URLLoader = new URLLoader();
var cssRequest:URLRequest = new URLRequest("css.css");
var styles:StyleSheet = new StyleSheet();
cssLoader.load(cssRequest);
cssLoader.addEventListener(Event.COMPLETE, onCSSComplete);
function onCSSComplete(e:Event):void {
styles.parseCSS(cssLoader.data);
xmlLoader.load(new URLRequest("xml.xml"));
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
trace ("CSS has loaded.");
}
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
parseFile(xmlData);
}
function parseFile(xmlContent:XML):void {
stage.addChild(container);
var contents:TextField = new TextField();
contents.styleSheet = styles;
contents.htmlText = xmlContent.slide.content.text()[slide];
contents.width = xW;
contents.x = xSet;
contents.y = ySet;
contents.antiAliasType = "advanced";
contents.multiline = true;
contents.autoSize = "left";
contents.wordWrap = true;
container.addChild(contents);
}
The text appears on the stage, but with no formatting. I'm not getting any errors. If anyone can help with this it would be greatly appreciated.

This topic has been closed for replies.
Correct answer

The default parser doesn't like the charset decl in css:

@charset "utf-8";

Remove line from the file and you should be ok. Alternatively, you could write your own parser -- or strip the declaration out.

1 reply

Correct answer
June 9, 2010

The default parser doesn't like the charset decl in css:

@charset "utf-8";

Remove line from the file and you should be ok. Alternatively, you could write your own parser -- or strip the declaration out.

webby7097Author
Inspiring
June 9, 2010

Fabulous!! Thank you.

June 9, 2010

You're welcome.