PDA

View Full Version : Question for loading info in XML. (beginner)



andriusain
November 25th, 2008, 02:06 PM
Hello everyone!

I am a beginner on AS3 and I got stuck accessing multiple data on an XML file. It should be pretty simple to sort out but I do not seem to be able to find the information by myself so unfortunately I have to resort to you wise people.

Hopefully you would have some spare seconds to help me out.

my XML code is pretty simple as follows:


<?xml version="1.0" encoding="iso-8859-1"?>
<projects>

<item>
<date>2007</date>
<desc>Barra Nova Golf Resort Ilheus - Bahia, Brazil.</desc>
<image>1BarraNova</image>
<place>Bahia, Brazil.</place>
</item>

<item>
<date>2007</date>
<desc>Lagoa Pearl Eco-Friendly Resort</desc>
<image>2lagoa</image>
<place>Bahia, Brazil.</place>
</item>

<item>
<date>2007</date>
<desc>Desert Pearl Beach Apartments - Hurghada, Egypt</desc>
<image>3desertPearl</image>
<place>Hurghada, Egypt</place>
</item>

</projects>And my AS3 code is as follows:


var projectsURL:URLRequest=new URLRequest("projects/projects.xml");
var xmlLoader:URLLoader=new URLLoader(projectsURL);
xmlLoader.addEventListener(Event.COMPLETE,xmlLoade d);

var projectsXML:XML = new XML();
projectsXML.ignoreWhitespace=true;
var linkText:TextField=testo_txt;

function xmlLoaded(evt:Event):void {
projectsXML=XML(xmlLoader.data);
}

linkText.htmlText='Link: <a href="event:">Click</a>';
addChild(linkText);
linkText.addEventListener(TextEvent.LINK, linkEvent);

function linkEvent(evt:TextEvent):void {

myTextField.text= projectsXML.item[0].desc;
mySwfFile.source="projects/"+projectsXML.item[0].image+".swf";

}I omitted quite a lot of code inside the "linkEvent" function that is not relevant for the case but it all works fine. I have an external text file that is loaded into a text field where there are several links that load images and text when clicked.
The problem I have is that I am not able to direct the current html link clicked to retrieve the relevant information on the XML. I can do it for a single one as the code above shows but not for the relevant one. I have a hint of how this might be done but after many hours trying I have not been able to.
Just in case it helps here I show you the links made on the text file

<a href="event:1BarraNova">Barra Nova Golf Resort Ilheus - Bahia, Brazil.</a>
<a href="event:2lagoa">Lagoa Pearl Eco-Friendly Resort Ilheus - Bahia, Brazil</a>
<a href="event:3desertPearl">Desert Pearl Beach Apartments - Hurghada, Egypt</a>Thank you for helping me out if you did!

creatify
November 25th, 2008, 02:30 PM
You're going to need a "key" of some sort that will relate your text link to the index or name of an item in the XML.

But, before going into all that, why not place your textlinks in the XML - why are you utilizing a separate file for that? It would be a bit easier if it was all in one XML file. If this is possible, let use know, if not, I think I can help with your current system.

andriusain
November 25th, 2008, 02:51 PM
You're going to need a "key" of some sort that will relate your text link to the index or name of an item in the XML.

But, before going into all that, why not place your textlinks in the XML - why are you utilizing a separate file for that? It would be a bit easier if it was all in one XML file. If this is possible, let use know, if not, I think I can help with your current system.

Well... basically the text file that I am linking is a really long CV file, and on that CV there are several projects that I want to link so that when they are clicked a swf image viewer/slideshow will display plus several text fields with different information that is retrieved from the xml. Does it make sense?

Thank you for your support Creatify.

creatify
November 25th, 2008, 04:00 PM
Well... basically the text file that I am linking is a really long CV file, and on that CV there are several projects that I want to link so that when they are clicked a swf image viewer/slideshow will display plus several text fields with different information that is retrieved from the xml. Does it make sense?

Thank you for your support Creatify.


Hmmm, I'd really recommend simply adding all the cv content to the xml file - none the less, this is doable - you have access to modify (add to) the cv file and the xml file correct?

If so, I'll try to assist w/ a solution.

andriusain
November 26th, 2008, 05:33 AM
Yeah sure I have access. Also note that my text file is being formatted through css.

I actually found a simple solution which was passing the node instead of the names I was passing and using the evt.text property.



linkText.htmlText='Link: <a href="event:0">Click</a>';

function linkEvent(evt:TextEvent):void
{
myTextField.text = projectsXML.item[Number(evt.text)].desc;
mySwfFile.source = "projects/" + projectsXML.item[Number(evt.text)].image + ".swf";
}

However I am interested in what you say cause I could probably save using a file and include the CV in the XML, providing that it could still be formatted through CSS.

I never knew this could be done though.

Next thing I want to do is to create a button that goes to the next slide.

I tried the following and did not work:


myTextField.text = projectsXML.item[Number(evt.text + 1)].desc;
mySwfFile.source = "projects/" + projectsXML.item[Number(evt.text + 1)].image + ".swf";

suggestions appreciated.

Thanks again.

JTF2
November 26th, 2008, 01:19 PM
Try adding one outside of the Number cast



myTextField.text = projectsXML.item[Number(evt.text) + 1].desc;
mySwfFile.source = "projects/" + projectsXML.item[Number(evt.text) + 1].image + ".swf";

andriusain
November 26th, 2008, 01:57 PM
JTF2 what your suggestion will actually do is go to node number 1, but no further from there and only will work for the swf file and not for the text field. Strange.

I'm thinking that what I should actually do is store a variable for the current node. But don't know how to do that either.

Thanks.

JTF2
November 26th, 2008, 06:27 PM
One way to do this is to create a variable to keep track of the index of your XMLList. In the event handler of your next button, you could increment this index variable and then use the variable within the code



var index:int = 0;

nextBtn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void {
if(index < projectsXML.item.length()) {
myTextField.text = projectsXML.item[index].desc;
mySwfFile.source = "projects/" + projectsXML.item[index].image + ".swf";
index++;
}
}

andriusain
November 27th, 2008, 07:59 AM
That's great JTF2, thank you!

I added this little bit so that it loops when it reaches the end:


else if(index == projectsXML.item.length()){
mySwf.source = "projects/" + projectsXML.item[index = 0].image + ".swf";
}

However a reason why your suggestion would not fully work in this particular case is because the index count might not always start at 0. If you remember I have several links in a text file that will link each one to a particular stage in the slide show. In the event.text property I passed the node numbers for each link starting from 0. So we could probably say something like:


index:String = event.text;

The problem is that index++ will not work for that. I would need some other code if there is any in order to add 1 to a string. I do not know.

But thanks I'll try to figure it out.

andriusain
November 27th, 2008, 08:21 AM
Oh yes of course, got it.

this would do it:


var index = [Number(evt.text)];However I don't know why there is a problem still.

So the slide show comes up, on whichever slide, then if I click the next slide button the same slide comes up, but if I click it more times then everything works fine. So I need to get rid of the same slide coming up on the first click.

andriusain
November 27th, 2008, 08:25 AM
And here you go:


var index = [Number(evt.text) +1];

Seems I am talking to myself (lol)!:blah:

Thank you all!

andriusain
November 27th, 2008, 02:45 PM
Ok but do you know why actually the text would not load? It just disappears.



var index = [Number(evt.text) +1];

nextBtn.addEventListener(MouseEvent.CLICK, onClick);

function onClick(e:MouseEvent):void {
if(index < projectsXML.item.length()) {
myTextField.text = projectsXML.item[index].desc;
mySwfFile.source = "projects/" + projectsXML.item[index].image + ".swf";
index++;
}
}