A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: parse data problem ???

  1. #1
    Senior Member
    Join Date
    Jan 2004
    Posts
    121

    parse data problem ???

    ok so i have an xml file:
    http://suckmyrazor.org/blog/flash.xml
    with this structure:
    Code:
                            blog
                              |
                              |
                   /     /    |     \     \
                 item  item  item  item  item 
                              |
                  [with each item having:]    
                              |
      /        /         /    |         \             \
    title   content  author  date  commentsCount  commentsLink
             ??|??
         ??[content]?? 
    //bizarely the content node's contents are a drop down 
    (when viewed in IE) which i presum means it's contents 
    are it's own node? maybe not
    and i also have an actionscript function which loops through the xml making all the nodes into objects with properties. (kidly provided by JHarlequin) see here:
    http://www.flashkit.com/board/showthread.php?...

    but the only problem is that i have five nodes with the same name from the same parent, what i'd like to do is get the actionscript to asign a number at the end of the object name (the count number; i) so that each item node will be an individual object and not keep overwriting itself. Either that or make an array so that i can call item[1].title etc.

    the only problem is i don't have a huge amount of experiance with AS, i know how to do stuff, but i need it to be explained to me, the script just dosen't make sense to me beyond what i expressed in the above thread.
    please help!

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    This is my usual way of doing an XML parser of the kind you describe.

    I usually tend to ignore the enclosing 'blog' tag, since there is only one of them. I recursively parse and look for 'item' tags, and then build up an array of items.

    Code:
    
    myItems = [];
    
    nbrItems = 0;
    
    loadNode = function(it)
    {
      if (it.nodeName == "item") 
      {
        var item = [];
    
        item.title = it.attributes['title'];
        item.content = it.attributes['content'];
        item.author = it.attributes['author'];
        item.date = it.attributes['date'];
        // parse other fields here...
    
        myItems[nbrItems] = item;
        nbrItems++;
      }
      if (it.hasChildNodes()) {
        for (var i = 0; i < it.childNodes.length; ++i)
          loadNode(it.childNodes[i], lev+1);
      }
    }
    
    
    var myxml = new XML();
    
    myxml.onLoad = function()
    {
      loadNode(this);
      // finish up here...
    
      // Now you can access the items here...
      // for example:  myItems[2].title
    }
    
    myxml.load("mydata.xml");
    
    // Do not attempt to access the data here, it's not loaded yet, because myxml.load() is an asynchronous function.
    It loads the data into 'myItems' which are indexed by item number. Each item has the fields you need.

    To refer to the title of the 3rd item, say

    myItem[2].title;

    Hope this helps.

    - Jim

  3. #3
    Senior Member
    Join Date
    Jan 2004
    Posts
    121
    hey thanks for the reply, and thanks for your help, now i can see that this one should work, (it looks like it's doing the right stuff in the right places) but i think i must be doing something wrong!

    if i check the variables when checking movie the output window is so:

    Code:
    Variable _level0.myItems = [object #1, class 'Array'] [
        0:[object #2, class 'Array'] [
          title:undefined,
          content:undefined,
          author:undefined,
          date:undefined
        ],
        1:[object #3, class 'Array'] [
          title:undefined,
          content:undefined,
          author:undefined,
          date:undefined
        ],
        2:[object #4, class 'Array'] [
          title:undefined,
          content:undefined,
          author:undefined,
          date:undefined
        ]
      ]
    i tried trace(myItems.item[1].title);

    but it just said undefined, i noticed that the code said attributes in it somewhere, could it be that it's trying to assign attributes of the nodes to the array? beacuse the is actually in children nodes. is this it? thank you so much for your help though. and i love the stuff on your site! i spent 2hours looking through all that and now my boss is a litlle angry! lol it's good stuff. thanks again.

    p.s i can post file if needs be.

  4. #4
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    It will help if you post a sample of your XML file. I was assuming you were using tag properties (which are easier to parse) but since you're not, let's see what your XML looks like....

    - Jim

  5. #5
    Senior Member
    Join Date
    Jan 2004
    Posts
    121
    of course, you can find the file here:

    http://suckmyrazor.org/blog/flash.xml

    i would offer to post it, but it's generated by the movabletype blogging engine, if you need a copy locally you could always right click save i suppose.

    many thanks.

  6. #6
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    This parses your file, and traces the values.


    Code:
    // XML Loader
    
    items = new Array();
    var curItem;
    var inContent = false, inBlog = false;
    
    loadNode = function(it)
    {
    	var isContentNode = false;
    	var isBlogNode = false;
    	
    	if (it.nodeName == 'blog') {
    		inBlog= true;
    		isBlogNode = true;
    	}
    	else if (inBlog) 
    	{
    		if (it.nodeName == 'item') {
    			trace("got item");
    			curItem = new Array();
    			items.push(curItem);
    		}
    		else if (it.nodeName == "title") 
    		{
    			curItem.title = it.firstChild.nodeValue;
    		}
    		else if (it.nodeName == "content") 
    		{
    			isContentNode = true;
    			inContent = true;
    			curItem.content = "";
    		}
    		else if (it.nodeName == "author") 
    		{
    			curItem.author = it.firstChild.nodeValue;
    		}
    		else if (it.nodeName == "date") 
    		{
    			curItem.date = it.firstChild.nodeValue;
    		}
    		else if (it.nodeName == "commentsCount") 
    		{
    			curItem.commentsCount = it.firstChild.nodeValue;
    		}
    		else if (it.nodeName == "commentsLink") 
    		{
    			curItem.commentsLink = it.firstChild.nodeValue;
    		}
    		else if (inContent) {
    			trace("got content " + it.nodeType);
    			curItem.content += it.nodeValue;
    		}
    	} // end inBlog
    	if (it.hasChildNodes()) {
    		for (var i = 0; i < it.childNodes.length; ++i)
    			loadNode(it.childNodes[i], lev+1);
    	}
    	if (isContentNode)
    		inContent = false;
    	else if (isBlogNode)
    		inBlog = false;
    }
    
    var myxml = new XML();
    
    myxml.onLoad = function()
    {
    	items = new Array();
    	loadNode(this);
    
    	// debugging
    	trace("Loaded nbrItems: " + items.length);
    	for (var i = 0; i < items.length; ++i) {
    		trace("item " + (i+1));
    		trace("  title   : " + items[i].title);
    		trace("  content : " + items[i].content);
    		trace("  author  : " + items[i].author);
    		trace("  date    : " + items[i].date);
    		trace("  cCount  : " + items[i].commentsCount);
    		trace("  cLink   : " + items[i].commentsLink);
    	}
    }
    
    myxml.load("flash.xml");
    // Do not attempt to access data here, it is not loaded at this point
    Send a double-latte to me at the website below.

    The format would have been a lot easier to parse (i.e. closer to my above, shorter post) if everything except 'content' were implemented as tag-properties on 'item'. e.g.

    Code:
    <item title="title" date="date" author="author" commentCount=5 commentLink="url">
      content goes here
    </item>


    - Jim
    Last edited by jbum; 03-24-2004 at 02:43 PM.

  7. #7
    Senior Member
    Join Date
    Jan 2004
    Posts
    121
    oh my god, thank you so so much! it's brilliant! the only slight couple of problems i have are in a. accessing the data, ie. how can i trace all of the data out to one big text box? and format it by using new lines etc? and b. i'm going to have another 3 maybe 4 xml files which will need loading at different points around my movie. could you explain where to make the changes so that i can use it with other xml files? oh and a double latte is on its way :P

    many thankx

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