A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Bit confused.

  1. #1
    Registered User
    Join Date
    Jun 2000
    Location
    UK, Exeter
    Posts
    350
    Here is the contents of a small test xml file I created, - note i've just replaced the square brackets with standard ones so it'll show up on the board.

    Code:
    (?xml version="1.0" encoding="ISO-8859-1"?)
    (xmlbody)
       (userdata)
          (name)Joe Bloggs(/name)
          (age)20(/age)
          (email)J.Bloggs@something.com(/email)
       (/userdata)
    (/xmlbody)
    Here is the actionscript I am using, (taken from a tutorial I found here somewhere but altered a bit)-

    ------------------------------------------------------------
    Code:
    myXML = new XML();
    myXML.onLoad = convertXML;
    status = "Loading data...";
    myXML.load("xmltest.xml");
    function convertXML () {
        if (this.loaded) {
            status = "Data loaded.";
        }
        mainTag = new XML();
        elementTag = new XML();
        articleList = new Array();
        elementList = new Array();
        // first get a handle on the first actual element in the document.
        // note we skip to the nextSibling element, since the first element
        // is a document definition tag.
        mainTag = this.firstChild.nextSibling;
        // make sure we have the right parent tag.  Do this by looking at the nodeName property
        // for this object.  This will correspond exactly to the <nodeName>, where nodeName is replaced
        // with the name of your xml tag
        if (mainTag.nodeName.toLowerCase() == "xmlbody") {
            // if we have a match, we collect all of the articles beneath it as an array of xml objects
            articleList = mainTag.childNodes;
            // now we loop over all the articles and look for the tags we are looking for...
            status = "";
            for (i=0; i<=articleList.length; i++) {
                // initialize a couple of variables to hold xml data we want displayed
                name = "";
                age = "";
                email = "";
                if (articleList[i].nodeName.toLowerCase() == "userdata") {
                    // we get the child node array beneath the articles, aka the meat and potatoes we are after
                    elementList = articleList[i].childNodes;
                    // and loop through that looking for the data we need
                    for (j=0; j<=elementList.length; j++) {
                        elementTag = elementList[j];
                        elementType = elementTag.nodeName.toLowerCase();
                        if (elementType == "name") {
                            name = elementTag.firstChild.nodeValue;
                        } else {
                            if (elementType == "age") {
                                age = elementTag.firstChild.nodeValue;
                            }
                        }
                    }
                    mydata += "Data: "+name+"Age= "+age;
                }
            }
        }
    }
    ------------------------------------------------------------

    As you can see in my xml document, I do not have a document type definition, so logically I replaced the,

    mainTag = this.firstChild.nextSibling;

    with,

    mainTag = this.firstChild;

    Problem is, this doesn't work. It still requires the .nextSibling on the end, when clearly I shouldn't need it.

    What is going wrong here. Thanks for your help. Just thought I'd better get up to scratch on xml, having never touched it before.

    Regards,
    Shad0w
    [Edited by Shad0w on 06-01-2001 at 10:02 AM]

  2. #2
    Junior Member
    Join Date
    Jun 2001
    Posts
    1
    The following is the first child in the document

    (?xml version="1.0" encoding="ISO-8859-1"?)

    you are using nextsibling to pass by it to its next sibling, in this case {xmlbody}.

  3. #3
    Registered User
    Join Date
    Jun 2000
    Location
    UK, Exeter
    Posts
    350
    If that were true, then everything would be fine, unfortunately, it isn't.

    Having read through a couple of threads on this board I've discovered that Flash recognises white space/carriage returns as nodes, therefor it is seeing the first carriage return as the .firstChild element, not as you suggested the (?xml version="1.0" encoding="ISO-8859-1"?).

    Which means it is seeing the root tag (xmlbody in my case) as the .nextSibling

    This makes sense now. Still a pain in the but though.

    Cheers.

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