Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Problem: Accessing the Right Node & Attributes Text

Status
Not open for further replies.

Sammybobo

Programmer
Apr 4, 2003
87
US
I am working in flash 8 and using an xml file that looks like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<lessons>
<lesson no="1" book="1" title="The Tree" ref="Chapter 1" />
<lesson no="2" book="1" title="House" ref="Chapter 2" />
<lesson no="3" book="1" title="Tim’s Birthday Surprise" ref="Chapter 4" />
<lesson no="4" book="1" title="Where Have You Been?" ref="Library" />
<lesson no="5" book="1" title="The Great Finish" ref="Chapter 2" />
<lesson no="6" book="1" title="Super" ref="Chapter 10" />
</lesssons>

I have set some variables like this:
no=""
book=""
title=""
ref=""
var lsn=3

How could I use the "lsn" variable to obtain the value of all the attributes of lesson no 3 node to get the right values for the variables: no, book, title, and ref?

Final result should be

Thanks.
 
A simple example:

Load the XML:
[tt]//
var lessonsXML:XML = new XML();
lessonsXML.ignoreWhite = true;
lessonsXML.load("lessons.xml");
lessonsXML.onLoad = function(success:Boolean) {
if (success) {
trace("XML loaded");
} else {
trace("XML failed");
}
};
//[/tt]

A function to retrieve the data:
[tt]//
function getLesson(lessonNumber:Number) {
var n:Number = lessonNumber-1;
var lesson:XMLNode = lessonsXML.firstChild.childNodes[n];
var no:String = lesson.attributes["no"];
var book:String = lesson.attributes["book"];
var title:String = lesson.attributes["title"];
var ref:String = lesson.attributes["ref"];
trace("no: "+no);
trace("book: "+book);
trace("title: "+title);
trace("ref: "+ref);
}
//[/tt]

Then when you can call this function, e.g. [tt]getLesson(3)[/tt], you'll get the output:
[tt]
no: 3
book: 1
title: Tim's Birthday Surprise
ref: Chapter 4
[/tt]

Kenneth Kawamoto
 
Thanks much for your help, Kenneth. I'll give it a spin!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top