-
XML troubles...
Alright, sorry for all the problems.
What I have been working towards is making a menu that loads using xml. Then when a button is pressed, i want to assemble a new menu and tween it in. My problem has been going to a certain location in the xml to load all of the button below a certain one.
Code:
<?xml version="1.0" enocoding="UTF-8"?>
<Base>
<Spacer>
<MainButton Name="About Kkthxbai" Content="test.swf" >
<MainButton Name="test1" Content="test.swf" />
<MainButton Name="susubmenu" Content="test.swf" />
</MainButton>
<MainButton Name="Portfolio" Content="test.swf" />
<MainButton Name="Contact" Content="test.swf" />
<MainButton Name="Store" Content="test.swf" />
<MainButton Name="Inspiration" Content="test.swf" />
</Spacer>
</Base>
This is my xml just for testing purposes.
I have tried using xml[VariableHoldingXmlLocation].MainButton.length but if the variable held a string of "Spacer.MainButton[0]" it would not return 2 but instead look for a Spacer.MainButton[0] node. So if anyone has an idea on how to make it realize that periods are not part of the name please respond.
After failing with this, I have tried to search the xml by using the name attribute but have come up empty. After looking at 2 dozen E4X tutorials, I have tried using
Code:
var nodelist:XMLList = xml.descendants.(attribute("Name") == "About Kkthxbai");
trace(nodelist.length() );
But everytime I do this I still get 0 even though there is a node with the attribute of About Kkthxbai : <MainButton Name="About Kkthxbai" Content="test.swf" >
I have also tried var nodelist:XMLList = xml.descendants.(@Name == "About Kkthxbai");
So any help is appreciated on a better way to do this, or a fix to my searching xml.
-
Code:
<?xml version="1.0" enocoding="UTF-8"?>
<Base>
<Spacer>
<MainButton Name="About Kkthxbai" Content="testA.swf" >
<MainButton Name="test1" Content="testA1.swf" />
<MainButton Name="test2" Content="testA2.swf" />
</MainButton>
<MainButton Name="Portfolio" Content="testB.swf" />
<MainButton Name="Contact" Content="testC.swf" />
<MainButton Name="Store" Content="testD.swf" />
<MainButton Name="Inspiration" Content="testE.swf" />
</Spacer>
</Base>
Code:
var myMenu:XMLList;
var myXML:XML;
var subHolder:Sprite;
var myXMLLoader:URLLoader = new URLLoader();
myXMLLoader.load(new URLRequest("base.xml"));
myXMLLoader.addEventListener(Event.COMPLETE, processXML);
function processXML(e:Event):void {
var xml:XML=new XML(e.target.data);
myXML=xml.Spacer[0];
myMenu=myXML..MainButton;
makeMenu(myXML);
myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
myXMLLoader=null;
}
function makeMenu(menuXML:*, holder:Sprite=null):void {
for (var i:* in menuXML.MainButton) {
var btn:Sprite = new Sprite();
btn.name=menuXML.MainButton[i].attribute("Name");
btn.y=i*42;
btn.graphics.beginFill(0xCCFF00);
btn.graphics.drawRect(0, 0, 100, 40);
if (holder) {
holder.addChild(btn);
btn.addEventListener(MouseEvent.MOUSE_OVER, doOverSub);
} else {
addChild(btn);
btn.addEventListener(MouseEvent.MOUSE_OVER, doOver);
}
}
}
function doOver(e:Event):void {
if (subHolder) {
for (var j:int = subHolder.numChildren-1; j >=0; j--) {
subHolder.getChildAt(j).removeEventListener(MouseEvent.MOUSE_OVER, doOverSub);
subHolder.removeChild(subHolder.getChildAt(j));
}
removeChild(subHolder);
subHolder=null;
}
trace(e.currentTarget.name+" : "+myMenu.(attribute("Name") == e.currentTarget.name).attribute("Content"));
makeSub(e.currentTarget);
}
function makeSub(targ:*):void {
var nodelist:XMLList = myMenu.(attribute("Name") == targ.name);
if (nodelist.MainButton.length()>0) {
subHolder = new Sprite();
subHolder.x=targ.x+targ.width;
subHolder.y=targ.y+targ.height-12;
addChild(subHolder);
makeMenu(nodelist, subHolder);
}
}
function doOverSub(e:Event):void {
trace(e.currentTarget.name+" : "+myMenu.(attribute("Name") == e.currentTarget.name).attribute("Content"));
}