A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: XML output in flash problem

  1. #1
    Member
    Join Date
    Apr 2004
    Location
    Milton Keynes
    Posts
    85

    XML output in flash problem

    Hi

    I have a simple xml file loaded into flash 8 as below.

    <?xml version="1.0" encoding="UTF-8"?>
    <phoneBook>
    <contact id="1">
    <name>Sas Jacobs</name>
    <address>123 Some Street, Some City, Some Country</address>
    <phone>123 456</phone>
    </contact>
    <contact id="2">
    <name>John Smith</name>
    <address>4 Another Street, Another City, Another Country</address>
    <phone>456 789</phone>
    </contact>
    <contact id="3">
    <name>Jo Bloggs</name>
    <address>7 Different Street, Different City, UK</address>
    <phone>789 123</phone>
    </contact>
    </phoneBook>

    Using this code to loop through and display in a text box.

    xmlData = new XML();
    xmlData.ignoreWhite = true;
    xmlData.onLoad = myFunction;
    function myFunction(success:Boolean):Void {
    if (success) {
    //process data
    trace("loaded");
    Rootnode = this.firstChild;
    num = Rootnode.childNodes.length;
    trace(num);
    for (i=0; i<Rootnode.childNodes.length; i++) {

    Name = Rootnode.childNodes[i].childNodes[0].firstChild.nodeValue;
    Address = Rootnode.childNodes[i].childNodes[1].firstChild.nodeValue;
    Phone = Rootnode.childNodes[i].childNodes[2].firstChild.nodeValue;
    id = Rootnode.childNodes[i].attributes["id"];
    ans += "<b><br><br>"+ Name +"<br>" + Address +"<br>" + Phone + id + "<br><br>";
    my_text.text = ans;
    }
    } else {
    trace("Not Loaded");
    }
    }
    xmlData.load("address.xml");



    The problem is the first word is UNDEFINED then it prints out perfectly. How can I get rid of the UNDEFINED or more importantly where is it coming from the output is this.

    undefined

    Sas Jacobs
    123 Some Street, Some City, Some Country
    123 4561



    John Smith
    4 Another Street, Another City, Another Country
    456 7892



    Jo Bloggs
    7 Different Street, Different City, UK
    789 1233



    Thanks for any help.

  2. #2
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    working in MX6 here, I get similiar results to yours with the html tab enabled in the textfield properties.
    the workaround was deselecting the tab and setting it in script.

    ans = "<b><br><br>"+ Name +"<br>" + Address +"<br>" + Phone + id + "<br><br>";
    my_text.html = true;
    my_text.htmlText += ans;

    Why ?? Go figure !! Anyone enlighten me

    hope it works for you

  3. #3
    Member
    Join Date
    Apr 2004
    Location
    Milton Keynes
    Posts
    85
    Thank for the help the only way I found out of this was to set the var ans to ans = "";
    outside of the loop the is building the string. Odd ???

  4. #4
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    It's not odd. You are concatenating the lines in the loop but have to define the first value of the field to be empty before the loop begins. Flash 8 is unforgiving with things like this.

    Code:
    ans="";
    for (i=0; i<Rootnode.childNodes.length; i++) {
    
    Name = Rootnode.childNodes[i].childNodes[0].firstChild.nodeValue;
    Address = Rootnode.childNodes[i].childNodes[1].firstChild.nodeValue;
    Phone = Rootnode.childNodes[i].childNodes[2].firstChild.nodeValue;
    id = Rootnode.childNodes[i].attributes["id"];
    ans += "<b><br><br>"+ Name +"<br>" + Address +"<br>" + Phone + id + "<br><br>";
    my_text.text = ans;
    }

    You should refine the line to send the completed string as htmlText since you have tags (like Dog used above)
    Code:
    my_text.htmlText = ans;
    Last edited by Chris_Seahorn; 05-07-2006 at 07:22 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center