-
learning xml + flash, quick questions
hi
i'm pretty experienced with actionscript, but i'm taking my first baby steps with xml & flash. right now, i'm learning how to navigate my way thru the xml tree.
how can i access the 2nd page node that contains 'Illustration' without using this long, drawn out code?
Code:
xmlOutput = this.firstChild.firstChild.nextSibling.nextSibling;
also, why does
Code:
trace(this.firstChild.firstChild.hasChildNodes();
return a 'false' value? aren't the nodes called 'Page' child nodes of 'Site'?
my xml...
PHP Code:
<Site>Matt's Portfolio
<Page>Web Design
<Item>Bennink's
<ImagePath>/img/benninks.jpg</ImagePath>
<BigImagePath>/img/benninks_big.jpg</BigImagePath>
<Notes open="true">benninks blaahbalbhlahblahbalbhlahblahbalbhlahblahbalbhlahblahb</Notes>
</Item>
<Item>Cain's Dog House
<ImagePath>/img/cains1.jpg</ImagePath>
<BigImagePath>/img/cains2.jpg</BigImagePath>
<Notes open="true">cains txt here h b albhlahb l ahbalbhl.,.,mshfe ahblahb</Notes>
</Item>
</Page>
<Page>Illustration
<Item>december Frog
<ImagePath>/img/frog1_small.jpg</ImagePath>
<BigImagePath>/img/frog1_big.jpg</BigImagePath>
<Notes open="false">frogblaahhblahbalbhlahblahbalbhlahblahb</Notes>
</Item>
</Page>
</Site>
my as...
Code:
myXML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = DisplayData;
myXML.load("test2.xml");
function DisplayData(success)
{
if (success) {
// xmlOutput = this.firstChild.firstChild.nodeValue; //matt's portfolio
// xmlOutput = this.firstChild.firstChild.nextSibling.childNodes[2]; //cains
xmlOutput = this.firstChild.firstChild.hasChildNodes(); //false
// xmlOutput = this.firstChild.firstChild.lastChild; //null
} else {
gotoAndStop(2);
}
}
stop();
thanks in advance
-
Hi arniemg,
Here's my comments...
how can i access the 2nd page node that contains 'Illustration' without using this long, drawn out code?
code: xmlOutput = this.firstChild.firstChild.nextSibling.nextSibling ;
Try the following:
code:
// without helper methods:
xmlOutput = this.firstChild.childNodes[2];
// with helper methods:
xmlOutput = this.documentElement().selectNodes("Page")[1];
also, why does
code: trace(this.firstChild.firstChild.hasChildNodes();
return a 'false' value? aren't the nodes called 'Page' child nodes of 'Site'?
Have a look at this thread, basically the firstChild of the 'Site' element is a text node, the 2nd child is the 1st 'Page' element. It returned 'false' because the text node doesn't have any child nodes.
The extra helper methods you need for the example I gave are as follows:
code:
/*
XML, XMLNode prototype additions
*/
// safe way to get root node (document element)
XML.prototype.documentElement = function()
{
var xmlElement = this.firstChild;
while( xmlElement != null && xmlElement.nodeType != 1 )
{
xmlElement = xmlElement.nextSibling;
}
return xmlElement;
};
// text content of node (optional parameter of 'true' (boolean) to normalize all text nodes)
XMLNode.prototype.nodeText = function()
{
if( arguments.length = 1 && arguments[0] == true )
{
// normalize all text nodes
var strResult = "";
var xmlCursor = this.firstChild;
while( xmlCursor != null )
{
if( xmlCursor.nodeType == 3 )
{
strResult += xmlCursor.nodeValue + " ";
}
xmlCursor = xmlCursor.nextSibling;
}
return strResult;
} else {
// simple mode, just the firstChild
if( this.firstChild != null && this.firstChild.nodeType == 3 )
{
return this.firstChild.nodeValue;
} else {
return "";
}
}
};
// select a single node by name
XMLNode.prototype.selectSingleNode = function( nodeName )
{
for( var i=0; i < this.childNodes.length; i++ )
{
if( this.childNodes[i].nodeName == nodeName )
{
return this.childNodes[i];
}
}
return null;
};
// select an array of nodes by name
XMLNode.prototype.selectNodes = function( nodeName )
{
var nodeList = new Array();
for( var i=0; i < this.childNodes.length; i++ )
{
if( this.childNodes[i].nodeName == nodeName )
{
nodeList.push( this.childNodes[i] );
}
}
return nodeList;
};
An alternative function to handle the XML showing the helper functions:
code:
function DisplayData(success) {
if (success) {
trace( this.firstChild.firstChild.nodeValue );
//matt's portfolio
trace( this.documentElement().nodeText() );
//matt's portfolio
trace( this.firstChild.firstChild.nextSibling.childNodes[2] );
//cain's
trace( this.documentElement().selectNodes("Page")[0].selectNodes("Item")[1] );
//cain's
trace( this.firstChild.firstChild.hasChildNodes() );
//false
trace( this.documentElement().selectSingleNode("Page").ha sChildNodes() );
//true
trace( this.firstChild.firstChild.lastChild );
//null
trace( this.documentElement().selectSingleNode("Page").la stChild );
//cain's
} else {
gotoAndStop(2);
}
}
Hope this helps!
Tim Walters
Senior Developer
XML Evangelist
"XML isn't a language, it's a way of life!"
-
wow. thanks a bunch. those new functions make things alot easier.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|