XML Node Returns vaue null?

  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

I'm working on a little flash movie that reads in XML data. This is the following code I'm using:
Code: [ Select ]
function processXMLData(success){
    if (success){
        var rootNode=this.firstChild;
        var numberofNodes = rootNode.childNodes.length;
        var rndI = Math.ceil(Math.random() * (numberofNodes - 0 + 1)) + (0 - 1);
        var commentsNode=rootNode.childNodes[rndI]; //Random Number Generator
        trace(commentsNode.nodeValue);
    }else{
        //Could not load XML file.
    }
}
comments_xml = new XML();
comments_xml.ignoreWhite = true;
comments_xml.load('conversation.xml');
comments_xml.onLoad=processXMLData;
  1. function processXMLData(success){
  2.     if (success){
  3.         var rootNode=this.firstChild;
  4.         var numberofNodes = rootNode.childNodes.length;
  5.         var rndI = Math.ceil(Math.random() * (numberofNodes - 0 + 1)) + (0 - 1);
  6.         var commentsNode=rootNode.childNodes[rndI]; //Random Number Generator
  7.         trace(commentsNode.nodeValue);
  8.     }else{
  9.         //Could not load XML file.
  10.     }
  11. }
  12. comments_xml = new XML();
  13. comments_xml.ignoreWhite = true;
  14. comments_xml.load('conversation.xml');
  15. comments_xml.onLoad=processXMLData;


When I run this code everything works fine except in my trace window the value is null. My XML file code is here:

Code: [ Select ]
<?xml version="1.0" encoding="utf-8"?>
<conversations>
 <say>It&apos;s true</say>
 <say>ok?</say>
 <say>It&apos;s true</say>
 <say>aww, (k)</say>
 <say>:P</say>
 <say>hey</say>
 <say>Point Taken</say>
 <say>hehe :P</say>
 <say>lol</say>
 <say>&gt;_&lt;</say>
</conversations>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <conversations>
  3.  <say>It&apos;s true</say>
  4.  <say>ok?</say>
  5.  <say>It&apos;s true</say>
  6.  <say>aww, (k)</say>
  7.  <say>:P</say>
  8.  <say>hey</say>
  9.  <say>Point Taken</say>
  10.  <say>hehe :P</say>
  11.  <say>lol</say>
  12.  <say>&gt;_&lt;</say>
  13. </conversations>


If I simply trace the commentsNode I get a value (though it adds the opening and closing <say> tags) however why can't I simply pull out the text in between with nodeValue? I feel like I'm overlooking something really small.

Also I use entity references in my XML file to avoid conflicts. However, Actionscript isn't interpreting "&apos;" as '. Is there another method I need in order to invoke this change?

Thanks for the help in advance!
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

try this instead...
Code: [ Select ]
var commentsNode=comments_xml.firstChild.childNodes[rndI]; //Random Number Generator
trace(commentsNode.firstChild.nodeValue);
  1. var commentsNode=comments_xml.firstChild.childNodes[rndI]; //Random Number Generator
  2. trace(commentsNode.firstChild.nodeValue);
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

That did the trick. Any idea's on the entity references thing in the last part of my post?

Thanks a lot for your quick reply!
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

hmm two things on that. Make sure your textfield is html text.
Code: [ Select ]
textfield1.html = true;
textfield1.htmltext = "your text to display";
// instead of //
textfield1.text = "your text to display";
  1. textfield1.html = true;
  2. textfield1.htmltext = "your text to display";
  3. // instead of //
  4. textfield1.text = "your text to display";


If that works then your good to go. However I always recommend using the numerical equivalent instead of the name so you might have an issue there as well. Here's a good list http://www.w3schools.com/tags/ref_entities.asp
  • RockmanTV
  • Proficient
  • Proficient
  • RockmanTV
  • Posts: 348

Post 3+ Months Ago

actually instead of .htmltext on the second line you'd just use .text. Otherwise it worked like a charm. Thanks a lot!
  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

glad it works. Actually htmltext will work as well but I typed it wrong. It should be camel case so htmlText instead of htmltext.

Sorry bout that. Its a good thing it works either way.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 28 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.