A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Tracing selected values.....simple stuff

  1. #1
    []D[][]V[][]D
    Join Date
    Feb 2001
    Location
    Clearwater - FL
    Posts
    227

    Tracing selected values.....simple stuff

    Just getting comfortable with Flash and XML and then someone asks me to write some code that will trace the node value of node name “item” with attributes color=”red”, shape=”circle”, and area=”450” and trace the parent node.

    This would be easy enough to do from a database but I am drawing a blank on the right way to do this with XML in spite of some minor experience. All I can remember is that it is supposed to be pretty straight forward(lol). Most of the tutorials I have seen didnt do much for me. Any guidance on the right way?
    XML below....(forgive me: minus the "<" symbols....couldnt get XML to post as a string when they were there...(?).

    hotel>
    item color=”white”shape=”rectangle”area=”48 >bed/>
    item color=”brown”shape=”rectangle”area=”8” >table/>
    item color=”black”shape=”square”area=”4”>tv/>
    /hotel>

    school>
    item color=”yellow”shape=”rectangle”area=”120 0”>bus/>
    /school>

    office>
    item color=”blue”shape=”rectangle”area=”200 >sign/>
    item color=”red”shape=”circle”area=”60”>tab le/>
    item color=”square”shape=”square”area=”9”>p ainting/>
    /office>

    hospital>
    item color=”white”shape=”rectangle”area=”500 >ambulance/>
    item color=”red”shape=”cross”area=”250”>red cross/>
    /hospital>
    Last edited by Synic; 05-21-2003 at 01:57 AM.

  2. #2
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    First to get your XML to appear wrap [ php] [ /php] tags around it (without the space).

    As for your XML, in a full blown XML environment such as a server you can do it with XPath stuff. In flash you can do it by looping through every node to find the one that you want. For this sort of thing I use a recursive function call that searchs all the child nodes of a node for the one I want, and then call the same function on all the childNodes (if I didn't find the node I want).

    Thanks

    Luke
    Flash-XML FAQ
    http://www.tupps.com/flash/faq/

  3. #3
    []D[][]V[][]D
    Join Date
    Feb 2001
    Location
    Clearwater - FL
    Posts
    227

    thats why you dont post w/o coffee first.

    thx Tupps.
    So anyway, I had to knock the rust off of some XML books, but yeah I have it doing what I need. Walk the tree baby.

    Anyway, what I think my problem is, is interpreting the question that was posed to me. -Specificaly, this part:

    " trace the node value of node name “item” with attributes color=”red”, shape=”circle”, and area=”450” and trace the parent node."

    ...It seems like a trick question almost, becausue there is no node that has those exact attributes. More to the point, none of the possibilities have an "area" attribute of "450". So, I am writing that part of the question off as a likely typo. (sent to me via email). It seems likely that thie person wants me to return the office.table node since its attributes most closely match the question (color=red shape=circle area=60)
    PHP Code:
    <?xml version="1.0" encoding="iso-8859-1"?>
    <places>
    <hotel>
        <item color="white" shape="rectangle" area="48">bed</item>
        <item color="brown" shape="rectangle" area="8">table</item>
        <item color="black" shape="square" area="4">tv</item>
    </hotel>
    <school>
        <item color="yellow" shape="rectangle" area="1200">bus</item>
    </school>
    <office>
        <item color="blue" shape="rectangle" area="200">sign</item>
        <item color="red" shape="circle" area="60">table</item>
        <item color="square" shape="square" area="9">painting</item>
    </office>
    <hospital>
        <item color="white" shape="rectangle" area="500">ambulance</item>
        <item color="red" shape="cross" area="250">red cross</item>
    </hospital>
    </places>
    So my question is two-fold:
    When he says "trace the node value" does that mean the string "table", or the node value of the table item [1] inside office[2].


    And when he says "..and trace the parent node", again, what in the heck is he asking for here? I could see him asking for the value of the node "office", or [2], or the parent node name "places" or value [0].


    BTW this is a request from a snooty C++ guy who thinks Flash is a Powerpoint-ish app. This is more of a challenge than anything. Any ammo appreciated. 8]

  4. #4
    []D[][]V[][]D
    Join Date
    Feb 2001
    Location
    Clearwater - FL
    Posts
    227

    ...!

    OMFG. Tupps, I love your site man. What a great resource. I will be there hangin out if you need me. Great real world implementation of XML. Kudos.

  5. #5
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    If he is a C++ guy (especially on Windows) ask him to produce something with a slightly complex ui (like a button or a label), and get it to talk to a web server.

    Three weeks later he might have something for you, it will work, it probably will be really fast, but it will have taken 3 weeks of solid work. Of course this all depends on what sort of libraries he has behind him to build this sort of stuff.

    I have always been a great beleiver in using the right tool for the job, and am definately not a 'have a hammer so everything is a nail' type person. But that is what you have to be when working in/for small businesses
    Flash-XML FAQ
    http://www.tupps.com/flash/faq/

  6. #6
    []D[][]V[][]D
    Join Date
    Feb 2001
    Location
    Clearwater - FL
    Posts
    227
    What about adding XML attributes to an Ascruipt Array?
    I have been playing with this but cant get it to work. (employeeArray[i].xmlName = nodeHandler1[i].attributes.name; //doesnt work)
    I can walk the tree fine and can isolate node values to vars,but it wont take in an Ascript Array. This is based off of a corporation.xml with (3) child <employees> and various other attributes.
    Is there a best practise for something that is so common?

    If you want the headache, I have included the script I have so far below ....

    quiz2XML = new XML();
    quiz2XML.load("corporation.xml");
    quiz2XML.ignoreWhite = true;
    quiz2XML.onLoad = extractData;
    function extractData(success) {
    rootHandler = this.childNodes;
    //all
    rootHandlerName = this.firstChild.nodeName;
    //<corporation>
    for (j=0; j<rootHandler.length; j++) {
    nodeHandler1 = rootHandler[j].childNodes;
    //@employees
    //trace(nodeHandler1);
    nodeHandlerName = rootHandler[j].nodeName;
    //trace(nodeHandlerName);//corporation
    institutionType = nodeHandlerName;
    for (i=0; i<nodeHandler1.length; i++) {
    //trace(this.childNodes[i].nodeName);
    if (nodeHandler1[i].nodeName == "employee") {

    //Right here is where I attempted to create an Array on thye fly to be filled with node values. Doesnt work.
    employeeArray[i] = [xmlName, xmlAdd1, xmlAdd2, xmlCity, xmlState, xmlZip, xmlPhone, xmlPayRate]();

    employeeArray[i].xmlName = nodeHandler1[i].attributes.name;
    employeeArray[i].xmlAdd1 = nodeHandler1[i].attributes.address1;
    employeeArray[i].xmlAdd2 = nodeHandler1[i].attributes.address2;
    employeeArray[i].xmlCity = nodeHandler1[i].attributes.city;
    employeeArray[i].xmlState = nodeHandler1[i].attributes.state;
    employeeArray[i].xmlZip = nodeHandler1[i].attributes.zipcode;
    employeeArray[i].xmlPhone = nodeHandler1[i].attributes.phone;
    name[i] =nodeHandler1[i].attributes.name;
    trace(employeeArray[i]);
    trace(institutionType+"\n" + "Name: "+xmlName+"\n Address1:"+xmlAdd1+"\n Address2: "+xmlAdd2+"\n City:"+xmlCity+"\n State:"+xmlState+"\n Zip:"+xmlZip+"\n Phone:"+xmlPhone+"\n PayRate:"+xmlPayRate);
    }
    }

    }
    }

  7. #7
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    The attributes are already in an array, you access and play with them just like any array. Are you sure you have attributes?

    Thanks

    Luke
    Flash-XML FAQ
    http://www.tupps.com/flash/faq/

  8. #8
    []D[][]V[][]D
    Join Date
    Feb 2001
    Location
    Clearwater - FL
    Posts
    227

    all Attribs

    Yeah, in fact all I have is Attribues, thus some of my headache.
    Is there a way to get the length of the attribute list, like you can get childnodes.length? This way I can loop through each attribute in each record and add that attribute to a separate array?

    I am trying to make an Employee class that will use this XML to create 3 separate Employee objects. I just cant get the attributes into my class.

    PHP Code:
     <corporation>
      <
    employee name="john doe" address1="1234 Elm St." address2="Apt. 202" city="san diego" state="ca" zipcode="92125" phone="858-555-1234" payRate="9.25" /> 
      <
    employee name="jane doe" address1="9281 Palm" city="san diego" state="ca" zipcode="92123" phone="619-555-3343" payRate="17.85" /> 
      <
    employee name="joseph smith" address1="12 Aero Dr." address2="Suite. 1301" city="san diego" state="ca" zipcode="92126" phone="858-555-4545" payRate="9.25" /> 
      </
    corporation

  9. #9
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    Yep Attributes are an array so all the usual array commands like length, join etc work with it.

    Thanks

    Luke
    Flash-XML FAQ
    http://www.tupps.com/flash/faq/

  10. #10
    []D[][]V[][]D
    Join Date
    Feb 2001
    Location
    Clearwater - FL
    Posts
    227

    counting attributes...but really.

    ex:

    var d= mainNode.firstChild.attributes.length;
    --this doesnt work.

    ...I cant get counting attributes to work like counting nodes.

    Is there another way to tally up your attributes?
    I am working with an XML that has no node values and each record has 6 attributes.

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