External XML values problems .

  • classified
  • Expert
  • Expert
  • User avatar
  • Posts: 540
  • Loc: Bahrain

Post 3+ Months Ago

its been a while since i been here , new faces , fresh bloodz ... good to see that ...


okay , im loading an XML files that has values like 3.2 ,4.5 , 4.01 etc ...
storing them in an array like :
Code: [ Select ]
for (i=1; i<total; i++) {

  rating [i] =this.firstchild.childNodes[0].childNodes[i].childNodes[4].childNodes[0];
}

            
  1. for (i=1; i<total; i++) {
  2.   rating [i] =this.firstchild.childNodes[0].childNodes[i].childNodes[4].childNodes[0];
  3. }
  4.             


this works fine , i can iterate the array and trace them ...
but once i put them into a mathematical operation it just doesnt work
E.g

Code: [ Select ]
(rating[2] + rating[4])


, i wrote a comparison function e.g if rating[this.ID] > 2.3 then do sumthin
but it doesn't do anything nor throws an error ...

and whenever i try to subtract multiply or add values to it it gives NaN ( Not a Number )

im rusty and ran outta ideas ...
ideas ?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Code: [ Select ]
childNodes[N]

That will return an XMLNode.

You'll either need to access attributes,
Code: [ Select ]
childNodes[N].attributes.attribute_name


Or what is usually overlooked by people when using XML, which is that the contents of a node, is itself a node.
Code: [ Select ]
<xml.firstChild>
  <childNodes[0]> firstChild.nodeValue </childNodes[0]>
</xml.firstChild>
  1. <xml.firstChild>
  2.   <childNodes[0]> firstChild.nodeValue </childNodes[0]>
  3. </xml.firstChild>


Which would make it,
Code: [ Select ]
childNodes[0].firstChild.nodeValue

Though childNodes[0] could very well have been your way of accessing firstChild.
My confusion from looking at that code is why I always use firstChild instead of childNodes[0]

Since you're working with numbers, you'll want to make sure you parse them to numbers, xml nodeValue is always a string unless parsed otherwise.
Code: [ Select ]
var myNumber:Number = parseFloat( childNodes[0].firstChild.nodeValue );
  • classified
  • Expert
  • Expert
  • User avatar
  • Posts: 540
  • Loc: Bahrain

Post 3+ Months Ago

you got a point there ...
the thing is the XML file im using is big , so i used Sepy actionscript editor to get the xml paths for me ...

i'll give it a go and will post updates later ...
  • classified
  • Expert
  • Expert
  • User avatar
  • Posts: 540
  • Loc: Bahrain

Post 3+ Months Ago

Ok Mr.My hero ... that solved the case , never thought that would actually affect how the array is handled ....

Code: [ Select ]
      rating [i] = this.firstchild.childNodes[0].childNodes[i].childNodes[4].childNodes[0].nodeValue;


well done mate & thanks .
  • krismeister
  • Graduate
  • Graduate
  • krismeister
  • Posts: 202

Post 3+ Months Ago

What you did was a fine way to do it, but next time you have such a complicated XML, maybe look into the XPATH class.

You can query the XML by node name and it will return an array of XML nodes.

You could go something like:
XPathAPI.selectNodeList(aLoadedXML.firstChild, "/food/fruit/apple/taste");

but in your example it looks like the star operator would help alot by going:
XPathAPI.selectNodeList(aLoadedXML.firstChild, "*/taste");
This returns an array of every <taste> node in the XML regardless of whose ChildNode it is.
  • classified
  • Expert
  • Expert
  • User avatar
  • Posts: 540
  • Loc: Bahrain

Post 3+ Months Ago

cheers buddy , true , i lose the way when huge xml data are involved ...
thanks for the linkage .

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 6 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.