Xml| News System
You can learn from this example:
1> How to load and parse xml;2> how to format text with Textfield.stylesheet
Look at the structure of the XML first:
<?xml version= "1.0" encoding= "UTF-8"?>
<csdeny>
<news date= "2006-02-03" title= "State Councilor Zhili" ><content>4 month 5th afternoon, State Councilor Zhili, Minister of Education </content></news>
<news date= "2006-02-03" title= "all Hair" ><content> Chinese tofu against the broad General Assembly against arc lamp Law </content></news>
<news date= "2006-02-03" title= "This is" ><content> our tofu against the broad General Assembly against arc lamp Law </content></news>
<news date= "2006-02-03" title= "tofu" ><content> Chinese tofu against the broad General Assembly against arc lamp Law </content></news>
<news date= "2006-02-03" title= "also extra" ><content> our tofu against the broad General Assembly against arc lamp Law </content></news>
<news date= "2006-02-03" title= "Shi da" ><content> our country tofu against the broad General Assembly against arc lamp Law </content></news>
<news date= "2006-02-03" title= "method" ><content> China tofu against the broad General Assembly against arc lamp Law </content></news>
</csdeny>
Here's how it works in Flash:
1> Create a new flash document, set the stage size (My settings are 150*200)
2> drag a textarea component into the stage, set the size and stage of the same size, instance named Mytextarea;
3> add code to the first frame:
var mystyles:textfield.stylesheet=new textfield.stylesheet ()//Declare Mystyles as StyleSheet class
Mystyles.setstyle (". Title",//Set caption display style
{fontFamily: ' song body, _sans ',
FontSize: ' 12px ',
Color: ' #003300 ',
TextAlign: ' Left '});
Mystyles.setstyle (". Date",//Set time display style
{fontFamily: ' Arial ',
FontSize: ' 9px ',
Color: ' #990000 ',
TextAlign: ' Right '};
Mystyles.setstyle (". Content",//set content display style
{fontFamily: ' song body, _sans ',
FontSize: ' 12px ',
Color: ' #003300 ',
TextAlign: ' Left '});
var newsxml:xml=new XML (); DECLARE XML class
Newsxml.ignorewhite=true; Ignore whitespace
Newsxml.onload=function (Success) {
if (success) {
Parsexml (); Call Parsexml () to parse XML if the XML is successfully loaded
}
}
Newsxml.load ("News.xml"); Loading external XML
The definition of function parsexml () {//parsexml ()
var e=newsxml.firstchild.childnodes;
var total=e.length;
Mytextarea.wordwrap=true; Set up textarea to wrap lines automatically
Mytextarea.editable=false; TextArea not editable
Mytextarea.hscrollpolicy=off; TextArea horizontal scroll bar close
Mytextarea.html=true; Supports XML
Mytextarea.stylesheet=mystyles; Call the previously set style
for (i=0;i<total;i++) {//parse XML and Format text
mytextarea.text+= "<.title>" +e[i].attributes.title+ "</.title>";
mytextarea.text+= "<.date>" +e[i].attributes.date+ "</.date>";
mytextarea.text+= "<.content>" +e[i].firstchild.firstchild.nodevalue+ "</.content><br>";
}
}
All right, that's easy.
PS: Can be extended to the mystyles, Flash support of the HTML tags are many:
Click here to download the source file