What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Messenger Plus! for Live Messenger » Scripting » Help with small XML reader

Help with small XML reader
Author: Message:
Matti
Elite Member
*****

Avatar
Script Developer and Helper

Posts: 1646
Reputation: 39
32 / Male / Flag
Joined: Apr 2004
RE: Help with small XML reader
How many tasks are in the tasks.xml? I can already see one issue, namely that you're starting from index 1 for your loop. In JScript (and actually all languages), arrays and collections start at index 0 so you're skipping the first element with that loop. I believe that you have just one task in the XML for testing this and therefore the for-loop immediately exits when it checks the condition.

Also, you don't need those IntTaskList and IntId variables. The length of the tasks list is only used once (for the loop condition) and the identifier is unnecessary and even incorrectly used. You're using it to determine the position of the inserted list-view item, however you're actually incrementing it three times for each item. For the first list-view, you increment the position, but you're also doing this for the second and third list-view.

The positions you expect:
code:
Task index   ListViewName   ListViewExpiration   ListViewCourse
   0            0              0                    0
   1            1              1                    1
   2            2              2                    2
   3            3              3                    3
The positions you'd get:
code:
Task index   ListViewName   ListViewExpiration   ListViewCourse
   0            0              1                    2
   1            3              4                    5
   2            6              7                    8
   3            9             10                   11
The correct way to get this would be:
js code:
    var IntId = 0;
    for (var i=0; i<Tasks.length; i++)
    {
        var TaskNode = Tasks[i];
        var NameNode = TaskNode.selectSingleNode("Name");
        var ExpiNode = TaskNode.selectSingleNode("Expiration");
        var CrseNode = TaskNode.selectSingleNode("Course");
>>>        Manage.LstView_AddItem("ListViewName", NameNode.text, IntId);<<<
>>>        Manage.LstView_AddItem("ListViewExpiration", ExpiNode.text, IntId);<<<
>>>        Manage.LstView_AddItem("ListViewCourse", CrseNode.text, IntId);<<<
>>>        IntId++;<<<
    }
If you look closely, you don't even need an extra variable for this. You can just use the i variable you declare in the for-loop.

And if you look even closer, you don't need anything of this at all. If you don't specify an insert position, Plus! will simply add the new item to the end of the list (also in the documentation). So, you don't need to give it a position and you definitely don't need IntId for this!

Some other remarks:
  • Since you're also using this function as refresh function when the window is already open, it is better to move the window creation outside the fillTable function. Otherwise, you'll be opening new windows every time you click Refresh.
  • Also for your refreshing, you need to clear the list-views before re-adding the items. The fastest way to do this is by sending LVM_DELETEALLITEMS to the three controls.
  • The for-loop can be optimized by storing the length of the collection and by using pre-incrementation instead of post-incrementation. This is most of the time not very important, but it's good practice to use efficient loops.

Thus, all of this together:
js code:
function OnEvent_MenuClicked(MenuItemId, Location, OriginWnd)
{
    // Captures when the user clicks on the 'WLMBb' menu
    if (MenuItemId === "Manage")
    {
        // Open the window
        var Manage = MsgPlus.CreateWnd("Interface.xml", "Manage");
        // Refresh the table
        fillTable();
    }
}

function CreateXML()
{
    try    {
        var xml = new ActiveXObject("Microsoft.XMLDOM");
    } catch(e) { try {
        var xml = new ActiveXObject("MSXML2.DOMDocument");
    } catch(e) { return; } }
    xml.async = false;
    return xml;
}

function OnManageEvent_CtrlClicked(PlusWnd, ControlId)
{
    if (ControlId == "Refresh")
    {
        // Refresh the table
        fillTable();
    } 
}

function fillTable()
{
    // Clear all list-views before adding new items
    var LVM_DELETEALLITEMS = 0x1009; // = LVM_FIRST + 9
    Manage.SendControlMessage("ListViewName", LVM_DELETEALLITEMS, 0, 0);
    Manage.SendControlMessage("ListViewExpiration", LVM_DELETEALLITEMS, 0, 0);
    Manage.SendControlMessage("ListViewCourse", LVM_DELETEALLITEMS, 0, 0);

    // Load the tasks from the XML file
    var XML = CreateXML();
    XML.load('tasks.xml');
    var Tasks = XML.selectNodes('/Tasks/Task');
    // Optimized the loop: start at 0, store length in len and use pre-increment
    for (var i=0, len=Tasks.length; i<len; ++i)
    {
        var TaskNode = Tasks[i];
        var NameNode = TaskNode.selectSingleNode("Name");
        var ExpiNode = TaskNode.selectSingleNode("Expiration");
        var CrseNode = TaskNode.selectSingleNode("Course");
        // No need to pass item position as parameter, Plus! will handle that
        Manage.LstView_AddItem("ListViewName", NameNode.text);
        Manage.LstView_AddItem("ListViewExpiration", ExpiNode.text);
        Manage.LstView_AddItem("ListViewCourse", CrseNode.text); 
    }
}

Note: Actually, there's still an even faster way to iterate through a node collection. Since all node objects are "truthy" (equal true), you can iterate through them with:
js code:
for(var i = 0, TaskNode; TaskNode = Tasks[i++]; ) {
    // ...
}
or using pre-incrementation:
js code:
for(var i = -1, TaskNode; TaskNode = Tasks[++i]; ) {
    // ...
}
However, that's some pretty advanced JScripting there. :P
Plus! Script Developer | Plus! Beta Tester | Creator of Countdown Live | Co-developer of Screenshot Sender 5

Found my post useful? Rate me!
05-26-2010 11:31 AM
Profile E-Mail PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Help with small XML reader - by chrisctx on 05-25-2010 at 05:36 AM
RE: Help with small XML reader - by matty on 05-25-2010 at 11:35 AM
RE: Help with small XML reader - by chrisctx on 05-25-2010 at 03:39 PM
RE: Help with small XML reader - by Matti on 05-25-2010 at 04:03 PM
RE: Help with small XML reader - by chrisctx on 05-25-2010 at 06:07 PM
RE: Help with small XML reader - by Matti on 05-25-2010 at 06:25 PM
RE: Help with small XML reader - by chrisctx on 05-26-2010 at 01:10 AM
RE: Help with small XML reader - by Matti on 05-26-2010 at 11:31 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On