A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: .nextSibling?????? maybe

  1. #1
    Senior Member
    Join Date
    Apr 2002
    Posts
    174

    .nextSibling?????? maybe

    Lil Help?,
    I am simply attempting to have a swf file pull content from an .xml file. I have had success pulling "attributes" but I would like to pull the content from "nodes". I think that's the terminology.

    Anywho, what I have going is 2 dynamic text boxs one labeled

    tablename

    and the second labeled

    bodycopy

    with the code provided below I get fine results within "tablename" however I get nothing in "bodycopy". Which leads me to believe the ".nextSibling" call/function whatever isn't working. Any help would be greatly appreciated.

    [fla code]

    tableXML = new XML();
    tableXML.load("XMLcondensed.xml");
    tableXML.onLoad = loadedXML;
    tableXML.ignoreWhite = true;
    function loadedXML() {
    rootNode = tableXML.firstChild;
    firstNode = rootNode.firstChild;
    tablenameNode = firstNode.firstChild;
    bodycopyNode = tablenameNode.nextSibling;

    tablename = tablenameNode.nodeValue;
    bodycopy = bodycopyNode.nodeValue;
    }
    stop();



    [xml code]

    <xml>
    <tablename>yoyo</tablename>
    <bodycopy>jojo</bodycopy>
    </xml>



    Thanks
    -j-

  2. #2
    Member
    Join Date
    Jan 2003
    Location
    Australia, Adelaide (SA)
    Posts
    97
    Hi jubjub,

    It sounds like you are using XML something like this:
    PHP Code:
    <myXML>
      <
    first>
        <
    tableName>My Table</tableName>
        <
    bodyCopy>More text heremaybe?</bodyCopy>
      </
    first>
    </
    myXML
    If the above is true, then your code is doing well right up until you use nextSibling().

    The issue is because the text ("My Table") is considered a child node... imagine the following document fragment:
    PHP Code:
    <sample>
      
    This is a <b>Sample</bof mixed content in <em>XML</em>
    </
    sample
    In the above XML the "sample" node has a few child nodes:

    1. type=text, value = "{Enter}{Tab}This is a "
    2. type=element, name = "b" with a child that is of type text with a value of "Sample"
    3. type=text, value = " of mixed content in "
    4. type=element, name = "em" with a child that is of type text with a value of "XML"
    5. type=text, value = "{Enter}"

    (you might not have the last child and the Enter and Tab in the first child depending on your IgnoreWhite settings).

    Because the above is possible, the text of a node has to be represented as a child node with a special type (nodeType=3 I believe) that has a .value of the text.

    What happened when you used nextSibling()? If you were using my "sample" then you would have ended up on the "b" element, but in the XML you are using it's not finding a sibling of the Text element, you need to use .parent.nextSibling() which will move the focus from the Text node back to the "tableName" node, and then find that node's nextSibling().

    To help avoid this confusion I wrote some helper functions for the XML object that allowed you to do things like:
    myXML.documentElement(); // select the root element
    myElement.selectSingleNode("nodeName"); // select the first child with a specific name
    myElement.selectNodes("nodeName"); // return an array of nodes that match the name
    myElement.nodeText(); // return the text of this element

    The last one was simply .firstChild.nodeValue with some error checking, although it could be extended to normalize multiple text-type children and return that...

    Let me know if you would like these functions and I'll see if I can dig them up.

    Hope this helps!
    Tim Walters
    Senior Developer
    XML Evangelist
    "XML isn't a language, it's a way of life!"

  3. #3
    Senior Member
    Join Date
    Apr 2002
    Posts
    174
    Actually my xml code is this.......


    <myXML>
    <tableName>My_Table</tableName>
    <bodyCopy>More_text_here,_maybe?</bodyCopy>
    </myXML>

    I don't know why it didn't show here in this thread but this is the xml I am using. My understanding was that both "tableName" and "bodyCopy" were childNodes of "myXML" that was the reasoning behind the usage of "nextSibling". Any code you could dig up would be fantastic.

    Thanks
    -j-

  4. #4
    Senior Member
    Join Date
    Apr 2002
    Posts
    174
    maybe I need to be in enhanced mode.

    testing....

    <myXML>
    <tableName>My_Table</tableName>
    <bodyCopy>More_text_here,_maybe?</bodyCopy>
    </first>
    </myXML>

  5. #5
    Senior Member
    Join Date
    Apr 2002
    Posts
    174
    well I don't know why the board is stripping my xml tags but my xml appears just as you had written minus the "first" tags you had included. That's to say both "tablename" and "bodycopy" are opened and closed independently within the "xml" tags. I apologize for the confusion.

    thanks
    -j-

  6. #6
    Member
    Join Date
    Jan 2003
    Location
    Australia, Adelaide (SA)
    Posts
    97
    To post XML wrap it in [ php ] and [ /php ] (without spaces), then your XML would come out as:
    PHP Code:
    <myXML>
      <
    tableName>My_Table</tableName>
      <
    bodyCopy>More_text_here,_maybe?</bodyCopy>
    </
    myXML
    Now, you are correct that the "tableName" tag is a child of "myXML", and the "bodyCopy" tag is also a child (and thus the nextSibling)... the issue is that the text "My_Table" is a CHILD of "tableName", perhaps if I show you a tree:
    Code:
    node: (name="MyXML", type=element)
      node: (name="tableName", type=element)
        node: (name=(null), type=text, value="My_Table")
      node: (name="bodyCopy", type=element)
        node: (name=(null), type=text, value="More_text_here,_maybe?")
    I'll say it again to be very clear Text inside an element is a node of type 3 (text-node), and is a child of the element. To access the text of the surrounding parent's sibling you need to go:
    .parent.nextSibling.firstChild.nodeValue

    Hope this helps!
    Tim Walters
    Senior Developer
    XML Evangelist
    "XML isn't a language, it's a way of life!"

  7. #7
    Senior Member
    Join Date
    Apr 2002
    Posts
    174
    Sounds good. Thanks for your help.

    -j-

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