PDA

View Full Version : looping through xml (newbie question)


annelie
02-09-2009, 11:27 AM
Hello,

I'm having some problems looping through my xml and getting the right values. If I've understood it correctly, this should give you every Line node in the xml document:

var xmlLines:XMLList = myXmlList..Line;

This returns nothing though, so I'm doing something wrong.

Here's the xml code:
<?xml version="1.0" encoding="UTF-16"?>
<Canvas Margin="5,12,0,0" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Canvas Tag="Measure">
<Line X1="0" Y1="0" X2="0" Y2="20" Stroke="Black" StrokeThickness="0.3" />
<Line X1="350" Y1="0" X2="350" Y2="20" Stroke="Black" StrokeThickness="0.3" />
<Canvas Tag="Column">
<Line X1="0" Y1="0" X2="350" Y2="0" Stroke="Black" StrokeThickness="0.3" />
<Line X1="0" Y1="5" X2="350" Y2="5" Stroke="Black" StrokeThickness="0.3" />
<Line X1="0" Y1="10" X2="350" Y2="10" Stroke="Black" StrokeThickness="0.3" />
<Line X1="0" Y1="15" X2="350" Y2="15" Stroke="Black" StrokeThickness="0.3" />
<Line X1="0" Y1="20" X2="350" Y2="20" Stroke="Black" StrokeThickness="0.3" />
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<Glyphs Tag="1Et" Width="14" Height="55.5" Fill="#FF000000" FontRenderingEmSize="14" UnicodeString="q" Canvas.Left="58" Canvas.Top="10" />
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<Glyphs Tag="1Et" Width="14" Height="55.5" Fill="#FF000000" FontRenderingEmSize="14" UnicodeString="q" Canvas.Left="116" Canvas.Top="-2.5" />
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<Glyphs Tag="1Et" Width="14" Height="55.5" Fill="#FF000000" FontRenderingEmSize="14" UnicodeString="h" Canvas.Left="174" Canvas.Top="2.5" />
</Canvas>
</Canvas>
<Canvas Tag="Measure">
<Line X1="350" Y1="0" X2="350" Y2="20" Stroke="Black" StrokeThickness="0.3" />
<Line X1="700" Y1="0" X2="700" Y2="20" Stroke="Black" StrokeThickness="0.3" />
<Canvas Tag="Column">
<Line X1="350" Y1="0" X2="700" Y2="0" Stroke="Black" StrokeThickness="0.3" />
<Line X1="350" Y1="5" X2="700" Y2="5" Stroke="Black" StrokeThickness="0.3" />
<Line X1="350" Y1="10" X2="700" Y2="10" Stroke="Black" StrokeThickness="0.3" />
<Line X1="350" Y1="15" X2="700" Y2="15" Stroke="Black" StrokeThickness="0.3" />
<Line X1="350" Y1="20" X2="700" Y2="20" Stroke="Black" StrokeThickness="0.3" />
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<Glyphs Tag="1Et" Width="14" Height="55.5" Fill="#FF000000" FontRenderingEmSize="14" UnicodeString="w" Canvas.Left="408" Canvas.Top="2" />
</Canvas>
</Canvas>
<Canvas Tag="Measure">
<Line X1="700" Y1="0" X2="700" Y2="20" Stroke="Black" StrokeThickness="0.3" />
<Line X1="1050" Y1="0" X2="1050" Y2="20" Stroke="Black" StrokeThickness="0.3" />
<Canvas Tag="Column">
<Line X1="700" Y1="0" X2="1050" Y2="0" Stroke="Black" StrokeThickness="0.3" />
<Line X1="700" Y1="5" X2="1050" Y2="5" Stroke="Black" StrokeThickness="0.3" />
<Line X1="700" Y1="10" X2="1050" Y2="10" Stroke="Black" StrokeThickness="0.3" />
<Line X1="700" Y1="15" X2="1050" Y2="15" Stroke="Black" StrokeThickness="0.3" />
<Line X1="700" Y1="20" X2="1050" Y2="20" Stroke="Black" StrokeThickness="0.3" />
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<Glyphs Tag="1Et" Width="14" Height="55.5" Fill="#FF000000" FontRenderingEmSize="14" UnicodeString="h" Canvas.Left="758" Canvas.Top="0" />
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />
<Glyphs Tag="1Et" Width="14" Height="55.5" Fill="#FF000000" FontRenderingEmSize="14" UnicodeString="h" Canvas.Left="874" Canvas.Top="0" />
</Canvas>
</Canvas>
</Canvas>

Here's the Flex code, I'm cheating a little bit to get to some of the line nodes, but ideally I would just like to be able to go through it all and call the relevant functions depending on what it is.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
initialize="initializeHandler()">

<mx:Script>
<![CDATA[
import mx.events.IndexChangedEvent;
import mx.core.UIComponent;
import mx.containers.VBox;

import components.DrawScore;

public var myXmlList:XMLList;

private function initializeHandler():void
{
var measure:DrawScore = new DrawScore();
var uic:UIComponent = new UIComponent();

myXmlList = scoreXml.children();

for (var i:uint = 0; i < myXmlList.length(); i++)
{
// isn't this supposed to give me all line nodes?
var xmlLines:XMLList = myXmlList..Line;

var canvasList:XMLList = myXmlList[i].children();

for (var iCanvas:uint = 0; iCanvas < canvasList.length(); iCanvas++)
{
// need another way to check if it's a canvas or a line (or a glyph), plus checking them for complexcontent
if (canvasList[iCanvas].hasComplexContent() == true)
{
// this is a canvas
}
else
{
// this is a line
var startX:int = canvasList[iCanvas].attribute("X1");
var startY:int = canvasList[iCanvas].attribute("Y1");
var endX:int = canvasList[iCanvas].attribute("X2");
var endY:int = canvasList[iCanvas].attribute("Y2");
var strokeColour:String = "Black";
var strokeThickness:int = canvasList[iCanvas].attribute("StrokeThickness");
var xmlNameSpace:String = canvasList[iCanvas].attribute("xmlns");

measure.createLine(startX, startY, endX, endY, strokeColour, strokeThickness, xmlNameSpace);
}
}

uic.addChild(measure);
}

var verticalBox:VBox = new VBox();
verticalBox.addChild(uic);
addChild(verticalBox);
}

]]>
</mx:Script>

<mx:XML id="scoreXml" source="assets/scoreCanvas.xml" />

</mx:Application>


I want to be able to call createLine for every line node, so if I can't use the code above, is there any other way of getting to it?

Many thanks,

Annelie

wvxvw
02-09-2009, 01:04 PM
See this thread:
http://www.actionscript.org/forums/showthread.php3?t=195916
Entry: "Q: How do I parse XML, that has names containing ":" (colons)?"

annelie
02-09-2009, 02:05 PM
Brilliant, thank you! I used the code in the entry below that to strip out the namespaces, have no need for them so that was a better solution.

Cheers,

Annelie