dcsimg
A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: xml.load problem (flash mx - os x)

  1. #1
    Member
    Join Date
    Sep 2003
    Posts
    39

    xml.load problem (flash mx - os x)

    hi,
    I'm not beeing able to read the contents of external xml files. I'm able to load them, but not read them. I'm using very standard code (pasted from the cookbook), ignoring white, and somehow always the output is either blank, undefined or null. The code for reading the xml tree works fine when the xml is created internally... would anybody have any thoughts? I'm using flash mx on mac os x. here's the code:

    Code:
    my_xml = new XML();
    my_xml.ignoreWhite = true;
    my_xml.onLoad = function (success) {
      if (success) {
        trace("The XML was successfully parsed.");
      } else {
        trace("There was an error parsing the XML data");
      }
    };
    
    my_xml.load("somefile.xml");
    
    for (var i = 0; i < children.length; i++) {
      trace(children[i]);
    }
    any help would be greatly appreciated

  2. #2
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    It should look more like this:

    PHP Code:

    my_xml 
    = new XML();
    my_xml.ignoreWhite true;
    my_xml.onLoad = function (success) {
      if (
    success) {
        
    trace("The XML was successfully parsed.");
    for (var 
    0this.children.lengthi++) {
      
    trace(this.children[i]);
    }

      } else {
        
    trace("There was an error parsing the XML data");
      }
    };

    my_xml.load("somefile.xml"); 
    the call to xml.load causes a call back (to the function you defined in onLoad()) to occur some time in the future. Your script keeps running while the XML data is being loaded.

    Otherwise flash would stop while your data loaded.

    Thanks

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

  3. #3
    Member
    Join Date
    Sep 2003
    Posts
    39
    thanks tupps, that makes sense and I've changed my code. but I'm still getting blank results

    here's my new code:

    Code:
    my_xml = new XML();
    my_xml.ignoreWhite = true;
    my_xml.onLoad = function (success) {
      if (success) {
        trace("The XML was successfully parsed.");   
    	rootElement = this.firstChild;
    	children = rootElement.childNodes;
    	for (var i = 0; i < children.length; i++) {
    	  trace('in for loop');
    	  trace(children[i]);
    	}	
      } else {
        trace("There was an error parsing the XML data");
      }
    };
    
    my_xml.load("somefile.xml");

  4. #4
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    your rootElement might not be this.firstchild. It is probably more likely this.lastchild. Also whitespace in XML appears as nodes, that is why most scripts loop through XML files.

    Also if you break into the debugger in flash you can traverse the XML tree in one of the variable panes.


    Thanks

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

  5. #5
    Member
    Join Date
    Sep 2003
    Posts
    39
    aaahh nice, the debugger really makes everything transparent!

    just before getting your post, though, I found out that the problem was with the xml formatting. flash didn't like this format:

    Code:
    <elements>
    	<element id=”1234-456-3434-34343” name=”testing1” type=”online”>
    		<title>Sample element</title>
    		<description>This is an element I just threw together</description>
    	</element>
    </elements>
    so now I'm looking into how to format xml that uses element properties so that flash can use it. but I'm on track.

    thanks tupps
    Oliver

  6. #6
    Member
    Join Date
    Sep 2003
    Posts
    39
    hmm, the vbcode didn't show the xml... here it goes as 'php' vbcode

    PHP Code:
    <elements>
        <
    element id=”1234-456-3434-34343” name=”testing1” type=”online”>
            <
    title>Sample element</title>
            <
    description>This is an element I just threw together</description>
        </
    element

  7. #7
    Member
    Join Date
    Sep 2003
    Posts
    39

    solved it

    i've figured out what's wrong with the xml - bad quotes! man I feel stupid after hours spent over this...

    -Oliver

  8. #8
    Senior Member tupps's Avatar
    Join Date
    Jan 2001
    Location
    Melbourne
    Posts
    2,035
    If you have problems verifying the XML open it in IE, IE does a pretty good job of identifying which line/item is causing the problems. Personally I also run the XML through an apache xml parser and it also spits out usefull (and sometimes different) error messages. Between the two one/both of them will have identified the offending item.

    Thanks

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

  9. #9
    Member
    Join Date
    Sep 2003
    Posts
    39
    good advice, I had forgotten about testing the xml outside flash... it'd be good if flash had a more robust parsing/validation

    listen, I have another question and I was wondering if I could take advantage of the thread to resolve it:

    with the data gathered from the xml file, I need to load multiple jpgs onto a scrollpane. The way I'm trying to do it is by using a single movieclip to be loaded by loadScrollContent(), attaching and positioning multiple container movieclips to it side by side, then loading the external jpgs on to them. I'm stuck in this last part:

    Code:
    my_xml.onLoad = function(success) {
    	if (success) {
    		rootElement = this.firstChild;
    		children = rootElement.childNodes;
    		imgContainerPosition = 0;
    		for (var i = 0; i< children.length; i++) {
    			imgContainerPosition = imgContainerPosition + 50;
    			imgName = children[i].attributes.name;
    			init = new Object();
        		        init._x  = imgContainerPosition;
        		        init._y = 50;
       			init.tryThis = function () { // GLITCH
         			this.loadMovie(imgName + '.jpg');
        		};
    			init.tryThis();
    			_root.attachMovie('imgContainer','img'+i,10+i,init);
    		}		
    	} else {
    		trace("There was an error parsing the XML data");
    	}
    };
    'tryThis()' is just my latest attempt at making this function execute after the movie is attached. I tried 'onLoad' and 'onEnterFrame', but they don't work... onload just gets ignored (I suppose because the mc already exists) and onEnterFrame only gets the last image looped through, since it executes after the loop.

    I posted this at the actionscript forum but no body responded... and I have to finish this for today! thus the oportunistic off-topic question :P

    thanks again

  10. #10
    Senior Member
    Join Date
    Feb 2001
    Posts
    1,835
    do the attach first, then change the parameters, similar to:

    code:
    _root.attachMovie("container","img" + i,i);
    obj = _root["img" + i];

    obj._x = i*50;
    obj._y = 50;
    obj.loadMovie(filename);


  11. #11
    Member
    Join Date
    Sep 2003
    Posts
    39
    hi enemem,

    Yes, I had figured this out on my own, and did exactly what you suggest, but forgot to post back here in my haste to finish the project. sorry about that!

    thanks anyway for replying and trying to help, I appreciate it.

    - 3rdw0rld3r

  12. #12
    Senior Member
    Join Date
    Feb 2001
    Posts
    1,835
    Originally posted by 3rdw0rld3r
    hi enemem,

    Yes, I had figured this out on my own, and did exactly what you suggest, but forgot to post back here in my haste to finish the project. sorry about that!

    thanks anyway for replying and trying to help, I appreciate it.

    - 3rdw0rld3r
    no probs just realised your last post was 18 days ago

    - n.

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