all groups > flash actionscript > october 2006 >
You're in the

flash actionscript

group:

XML id Search


XML id Search sbryner
10/12/2006 9:05:42 PM
flash actionscript:
Hey delving into XML and have a question. I'd like to create an array with some
of the attributes inside my XML elements. I'm probably far from what I'm trying
to do and very lost but here is my XML:

<?xml version="1.0"?>

<tools>
<saws id="1">
<name>Band saw</name>
<comment>created by craftsman</comment>
</saws>
<saws id="2">
<name>Jig Saw</name>
<comment>created by Stihl</comment>
</saws>

<hammers id="1">
<name>Big Bertha</name>
<comment>12 inches long</comment>
</hammers>

<hammers id="2">
<name>Harry Scary Hammer</name>
<comment>One scary hammer</comment>
</hammers>
</tools>



and here is my try at setting it up so each item is an array element...





var toolsXML:XML = new XML(); // create xml object
toolsXML.ignoreWhite = true; // ignores white space

toolsXML.onLoad = function(success) {
var tools:Array = this.firstChild.firstChild; // creates tools array
with xml objects
if(success){
for(var i=0;i<tools.length;i++) { // loops through
xml root element Tools
trace("tools "+tools.childNodes.attributes["id"]); // trace all tool
elements with attribute = "id"
}
}
}
toolsXML.load("tools.xml");
Re: XML id Search sbryner
10/12/2006 10:07:34 PM
updating my code... now how do I get it to search through each to retrieve only
the elements with the id=1 from my XML text?

how do I remove the properties <name> </name> and just retrieve the info
inside....

<?xml version="1.0"?>

<tools>
<saws id="1"> // first I'd like to search id for my variable.... say "1"
<name>Band saw</name> // or what if I want to retrieve just the
text "Band saw"
<comment>created by craftsman</comment>
</saws>

any thoughts?


var toolsXML:XML = new XML();
toolsXML.ignoreWhite = true;

toolsXML.onLoad = function(success) {
var tools:Array = this.firstChild.childNodes;
var newTools:Array = new Array();

if(success){
for(var i=0;i<tools.length;i++) {
newTools.push(this+i);
trace("Tools = "+newtools[i]);
}
}
};
toolsXML.load("tools.xml");
Re: XML id Search sbryner
10/19/2006 10:34:37 PM
ok, updating my file again... along with my question.

question now:

1) why when I try to add the XML childNodes from the info I'm searching for
(in this case "harry" from code: var toolName = "Harry";)
into "newTools" array by using the newTools.push(this[e]); it doesn't
add anything that I can see when
tracing (newTools)? Am I not adding in it correctly.

I'm trying to figure this all out using Kirupa.com's Introduction to XML just
need some specific answers.

thanks for helping,





var toolsXML:XML = new XML();
toolsXML.ignoreWhite = true;

toolsXML.onLoad = function(success) {
if(success){
toolSearch();
} // if (success)



}; //function


toolsXML.load("tools.xml");


toolSearch = function (){
var tools = toolsXML.firstChild.childNodes;
var newTools:Array = new Array();


for(var i=0;i<tools.length;i++) {
var theTools = tools[i];
var equ = theTools.childNodes;

for(var e=0; e<equ.length;e++){

var theName = equ[e].firstChild.firstChild.nodeValue;
var theComment = equ[e].lastChild.firstChild.nodeValue;


if(theName == toolName){

newTools.push(this[i]);
trace(newTools);

}else{

}//else
}//for i
}//for j
return (newTools);

}// function

var toolName = "Harry";
toolSearch();



// my XML file consists of : \\


<?xml version="1.0"?>

<tools>
<sawTools namer="saws">
<saws id="1">
<name>Band saw</name>
<comment>created by craftsman</comment>
</saws>
<saws id="2">
<name>Jig Saw</name>
<comment>created by Stihl</comment>
</saws>
</sawTools>

<hammerTools namer ="hammers">
<hammers id="1">
<name>Big Bertha</name>
<comment>12 inches long</comment>
</hammers>

<hammers id="2">
<name>Harry</name>
<comment>One scary hammer</comment>
</hammers>
<hammers id="3">
<name>Harry</name>
<comment>is a heavy hammer</comment>
</hammers>

</hammersTools>
</tools>
Re: XML id Search TimSymons
10/20/2006 1:12:25 PM
You should use the XPathAPI to help you with the searching.

mx.xpath.XPathAPI.selectSingleNode(startingNode, "pathToNode");

So for your example to get the <saws id="1"> node you could use:

mx.xpath.XPathAPI.selectSingleNode(theTools.firstChild,
"/tools/sawTools/saw[@id='1']");

To get the actual name of the saw at id='1' you could use:

var tempNode:XMLNode = mx.xpath.XPathAPI.selectSingleNode(theTools.firstChild,
"/tools/sawTools/saw[@id='1']");

trace(mx.xpath.XPathAPI.selectSingleNode(tempNode,
"/saws/name").firstChild.nodeValue);

Tim

Re: XML id Search sbryner
10/20/2006 9:25:16 PM
isn't the attached code close to what your saying if I wanted to search for the
<name> element of a node?
I found this with the flash help searching for xpath.

It doesn't go inside the "for loop" and I don't know why?

here's the code:



import mx.xpath.XPathAPI;

var toolsXML:XML = new XML();
toolsXML.ignoreWhite = true;


toolsXML.onLoad = function(success) {
trace("loading...");
if(success){

trace("loading successful...");

var toolPath = "/tools/saws/name"; //nodeValue I want to add to toolArray

var toolArray:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,toolPath);

for (var i=0; i<toolArray.length;i++){
trace("loading inside for loop...");
trace(toolArray[i].firstChild.nodeValue);
}
}else{
trace("error loading XML...");
}

}
toolsXML.load("tools.xml");
Re: XML id Search TimSymons
10/21/2006 12:36:26 AM
In the XML example code you posted your toolPath string should be:

"/tools/sawTools/saws/name"

Also, trace the length of the array to make sure you are getting a number.

Re: XML id Search sbryner
10/26/2006 4:02:52 PM
I'm lost. I can't seem to get my search to add to the elements to the length of
the (searchResult.length)

below is the code I'm working with now. I want to search the "saws" node in
the XML file
and return all the nodes within the "saws" node. so I used
[i]selectNodeList[/i].

I used a for loop to loop through and it returns the correct length of the
[i]toolArray[/]
though when I try to create a new array named [i]searchResult[/i] and add the
childNodes of
all "saws" childNodes it returns only "1".

I've increased the XML file to have 4 "saws" elements so the
returnSearch.length should = 4

also, I can't seem to get it to add nodeValues to the search..
How can I get it to look like this?

trace(searchResult[i])

returns something like.....

resultSearch:Array = {name: "band saw", comment :"The comment here"}];

Is this possible?





import mx.xpath.XPathAPI;

var toolsXML:XML = new XML();
toolsXML.ignoreWhite = true;


toolsXML.onLoad = function(success) {
trace("loading...");

if(success){
var toolPath = "/tools/sawTools"; //nodeValue I want to add to toolArray
var toolArray:Array =
mx.xpath.XPathAPI.selectNodeList(this.firstChild,toolPath);
theSearch(toolPath,toolArray);

}

}


sortName ="saws";
toolsXML.load("tools.xml");


function theSearch(toolPath,toolArray){

var searchResult:Array = new Array();
for (var i=0; i<toolArray.length;i++){
var theToolName = toolArray[i].firstChild.nodeName;

if(theToolName == sortName){
searchResult.push(this[i].firstChild.nodeValue);
trace(searchResult.length+" is the searchResult length");
trace(searchResult);
}else{
//trace("error loading XML...");

}//else
return (searchResult);
}// for
};//function
Re: XML id Search TimSymons
10/27/2006 3:34:54 PM
It's close. Here are a couple of ideas.

1) It seems that you are trying to re-produce some of the work that the
XPathAPI will do for you. Instead of getting just the main node, use the
XPathAPI to get all of the nodes you want. For example, if you want all of the
<saws> nodes then set your "toolsPath" string to: "/tools/sawTools/saws". The
code you used will give you an array of XML nodes. Each array element will be
one of the <saws> nodes.

2) The result that you posted is an object. The XPathAPI will not return
results like that. It will return actually XML nodes. If you want to convert
your XML node with attributes into an Object like you showed, then search
Adobe's Flash Exchange. It has a couple of code pieces (classes) that will do
that once you have the nodes you want.

I modified your code to show you how it will return the XML nodes in an array.
I also supplied how I thought the XML file looks (let me know if it looks
different). The last thing to note is that when you are referencing nodes in
your code you are off just a little. I can tell what you are looking for but
you are not using the right combinations of firstChild and nodeValue. Use the
"trace" function at every step and have it display the results. You may find
that the XMLNode properties take a little practice to get them to the path you
want.

Good luck with your coding.

Tim



import mx.xpath.XPathAPI;

stop();
var toolsXML:XML = new XML();
var toolArray:Array;
toolsXML.ignoreWhite = true;


toolsXML.onLoad = function(success) {
trace("loading...");
if(success) {
var toolPath = "/tools/sawTools/saws"; //nodeValue I want to add to
toolArray
toolArray = new Array();
toolArray = mx.xpath.XPathAPI.selectNodeList(this.firstChild,toolPath);
trace(toolArray);
//theSearch(toolPath,toolArray);
}
};

//sortName ="saws";
toolsXML.load("tools.xml");

// You shouldn't need the searchFunction, since the toolsArray will contain
what you need.
Re: XML id Search sbryner
10/31/2006 5:12:40 PM
thanks, TimSymons.

one more question... I found this on the Flash Exchange and it seems to not
want to go into the

XMLnode.prototype.setVars = function (prevObj) {

is there a noticable reason why?

XML.prototype.parseData = function (ok) {
if (ok) {
this.firstChild.setVars();
}
};


XMLnode.prototype.setVars = function (prevObj) {

if (this.hasChildNodes() && this.firstChild.nodeType == 1) {
if (prevObj == null) {
if (eval(this.nodeName) == null) {
this.nodeName = new Array(new Object());
} else {
eval(this.nodeName).push(new Object());
}
} else {
if (prevObj[this.nodeName] == null) {
prevObj[this.nodeName] = new Array(new Object());
} else {
prevObj[this.nodeName].push(new Object());
}
}
for (i in this.childNodes) {
thisVarName = eval(this.nodeName);
thisObj = prevObj == null ? thisVarName[thisVarName.length-1] :
prevObj[this.nodeName][prevObj[this.nodeName].length-1];
if (this.childNodes[i].hasChildNodes() &&
this.childNodes[i].firstChild.nodeType == 1) {
this.childNodes[i].setVars(thisObj);
} else if (this.childNodes[i].hasChildNodes() &&
this.childNodes[i].firstChild.nodeType == 3) {
thisObj[this.childNodes[i].nodeName] =
this.childNodes[i].firstChild.nodeValue;
}
}
}
};


myXML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = myXML.parseData;
myXML.load("toolsTwo.xml");
Re: XML id Search TimSymons
10/31/2006 8:06:51 PM
Not sure which version of Flash you are using but Flash 7 and higher are case
sensitive, so you need to make sure you use:

XMLNode

and not

XMLnode

They will mean different things to the compiler.

Tim
Re: XML id Search sbryner
11/1/2006 10:18:23 PM
Oh, Thanks, I run flash pro 8 and I got this from the exchange and it said it
was made in flash 4 I believe.

One more question:
the code is having an error and maybe this is due to the flash versions but
one part of the code in the setVar function seems to be pulling up an error
it's
the last line in this code:

if (this.hasChildNodes() && this.firstChild.nodeType == 1) {
if (prevObj == null) {
if (eval(this.nodeName) == null) {
eval(this.nodeName) = new Array(new Object()); // having problems here!


Thanks again, sky


Here is the code as a whole:



};

XMLNode.prototype.setVars = function (prevObj) {

if (this.hasChildNodes() && this.firstChild.nodeType == 1) {
if (prevObj == null) {
if (eval(this.nodeName) == null) {
eval(this.nodeName) = new Array(new Object());
} else {
eval(this.nodeName).push(new Object());
}
} else {
if (prevObj[this.nodeName] == null) {
prevObj[this.nodeName] = new Array(new Object());
} else {
prevObj[this.nodeName].push(new Object());
}
}
for (i in this.childNodes) {
thisVarName = eval(this.nodeName);
thisObj = prevObj == null ? thisVarName[thisVarName.length-1] :
prevObj[this.nodeName][prevObj[this.nodeName].length-1];
if (this.childNodes[i].hasChildNodes() &&
this.childNodes[i].firstChild.nodeType == 1) {
this.childNodes[i].setVars(thisObj);
} else if (this.childNodes[i].hasChildNodes() &&
this.childNodes[i].firstChild.nodeType == 3) {
thisObj[this.childNodes[i].nodeName] =
this.childNodes[i].firstChild.nodeValue;
}
}
}

};
Re: XML id Search sbryner
11/1/2006 10:27:52 PM
Also, I can't seem to get it to trace anything. I don't know why?
His example in his Help txt is

i.e.

<root>
<animal>
<type>mammal</type>
<name>
<first>Justin</first>
<last>Watkins</last>
</name>
</animal>
</root>

would turn into

root[0].animal[0].type = mammal;
root[0].animal[0].name[0].first = Justin;
root[0].animal[0].name[0].last = Watkins;


********************************************************************************

If there are several siblings with the same nodeName, the method adds those
elements to the end of the array. Since the method uses the for (.. in ..)
function all data is stored backwords in the array. The last element is
always
stored at arrayElement[0] and the first is arrayElement[array.length - 1]

I designed this for a very large and complex data object. It is very
effecient, and as long as the DTD of the XML is known or you have a basic
understanding of the heirarchy of the XML document, the created Flash data
object is easy to navigate and find the values.

********************************************************************************

Use by creating a new XML object. Load the XML and set the onLoad method to
the
parseData method.
myXML = new XML();
myXML.load("version1.xml");
myXML.onLoad = myXML.parseData;
Re: XML id Search TimSymons
11/2/2006 1:37:31 PM
Sky,

I tried to use the code you posted but I am not sure where this information is
getting stored. I can trace myXML but is spits out a lot of [object object]
items. When I try to access a particular node using the form he listed it did
not work, all I got was undefined.

I might be able to help more if you could let me know which one you pulled
down from the Exchange.

Tim
Re: XML id Search sbryner
11/2/2006 4:20:39 PM
Tim,

Here is the link to what I found it was published by Jwakins for Flash 5.


http://www.adobe.com/cfusion/exchange/index.cfm?view=sn110#loc=en_us&view=sn106&
viewName=Exchange%20Search%20Details&authorid=26176391&page=0&scrollPos=0&subcat
id=0&snid=sn106&itemnumber=3&extid=201541&catid=0

Hope this helps..

thanks again,
sky
AddThis Social Bookmark Button