Shoutbox

XML Reading/Parsing - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Messenger Plus! for Live Messenger (/forumdisplay.php?fid=4)
+---- Forum: Scripting (/forumdisplay.php?fid=39)
+----- Thread: XML Reading/Parsing (/showthread.php?tid=83792)

XML Reading/Parsing by Paril on 05-18-2008 at 01:48 PM

Hey there.

I'm attempting to grab an XML Feed from a stat page, record it's information and display certain parts of it. Is there any example code around for dealing with XML documents?

-Paril


RE: XML Reading/Parsing by mynetx on 05-18-2008 at 02:01 PM

Hello Paril,

first of all, your avatar URL is outdated: http://www.royalorigins.net/Portrait_Cloud.gif
Then, fetch the Official Scripting Documentation. It might help you.
Third, load the XML contents from its location. You can use the normal COM objects Microsoft.XMLHTTP and Microsoft.XMLDOM for it. Simply use
var objXml = new ActiveXObject("Microsoft.XMLDOM");
You will find more information about Microsoft.XMLDOM and its properties within the Microsoft Developers Network (MSDN) at http://msdn.microsoft.com/en-us/library/aa468547.aspx.
Regards,


RE: XML Reading/Parsing by Paril on 05-18-2008 at 02:04 PM

Thanks, site must be down. Oh well. Nothing big. Haven't been here in a while.

I had checked through there, not much that I saw that helped much.

I'll take a look, thanks.

-Paril


RE: XML Reading/Parsing by Paril on 05-18-2008 at 02:17 PM

I tried this just to test it:

code:
function OnEvent_Initialize(MessengerStart)
{
    var objXml = new ActiveXObject("Microsoft.XMLDOM");
   
    if (objXml.Load ("http://stats.enemyterritory.com/profile/paril101?xml=true"))
    {
        Debug.Trace("Made it!\n");
    }
    else
    {
    }
}

However I got an error on it.. Is there any example code lying around of an entire one used in MsgPlus? I'm not exactly sure how I'm supposed to.. interpret this code from (what looks like) VB.

-Paril
RE: XML Reading/Parsing by Matti on 05-18-2008 at 04:46 PM

MeEtc wrote a nice tutorial how to do this: [Tutorial] Communicating with web pages. Take a look at the section "Retrieving content of a web page" before you continue. It's really worth to read it! :)

The only difference is that you can use the xmlhttp.responseXML property rather than xmlhttp.responseText, as this contains a nice XMLDOM object, ready for you to edit!

Here you have an example on what you can do with it:

code:
var url = "http://stats.enemyterritory.com/profile/paril101?xml=true";
   
Interop.Call("wininet.dll", "DeleteUrlCacheEntryW", url);
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET", url, true);
xmlhttp.onreadystatechange = function () {
   if (xmlhttp.readyState == 4) {
      if (xmlhttp.status == 200) {
         // Data from URL was retrieved successfully
         // You can now use xmlhttp.responseXML
         var xml = xmlhttp.responseXML;
         Debug.Trace("Engineer XP:"+xml.selectSingleNode("player/xp/engineer").getAttribute("xp"));
      } else {
         // The server returned an HTTP code other than 200
         Debug.Trace("ERROR!");
      }
   }
}
xmlhttp.send();

RE: XML Reading/Parsing by Paril on 05-18-2008 at 09:59 PM

Ahh, that's perfect. Thanks. My uber-ness can take over from here. :p

-Paril


RE: XML Reading/Parsing by markee on 05-18-2008 at 10:40 PM

For a wiki with what xml stuff you can do with that object I use http://www.devguru.com/technologies/xmldom/quickr...mldom_objects.html

It also gives some vb and/or jscript examples with most of the properties or methods


RE: XML Reading/Parsing by Paril on 05-19-2008 at 12:14 AM

Okay, thanks.

Another question.. the ET:QW stats page also have some XML objects that are named the same, ie:

code:
<badges category="soldier" level="1">
<tasks total="600" id="1" value="8768.6899802"/>
<tasks total="2" id="2" value="87"/>
</badges>
<badges category="soldier" level="2">
<tasks total="3000" id="1" value="8768.6899802"/>
<tasks total="2000" id="2" value="2617.059387822"/>
<tasks total="20" id="3" value="87"/>
</badges>

How would I distinguish these apart, like getting the "total" under level "1" category "soldier" of task id "1"?

-Paril
RE: XML Reading/Parsing by Matti on 05-19-2008 at 03:36 PM

You can easily do that by using the right statements in your search parameter for xml.selectSingleNode():

code:
var node = xml.selectSingleNode("badges[@category='soldier'][@level='1']/tasks[@id='1']");
var total = node.getAttribute("total");
The parts in green are selectors for attributes, this means that the XMLDOM engine will first look for a node with the name 'badges' and then will check if it's category-attribute equals 'soldier' and the level is '1'. If this is true, it'll look in its child nodes for 'tasks'-nodes and if it finds one with id '1', it'll return that node. Then, you can simply use the Node.getAttribute("attribute_name") method to get the 'total' attribute from the node. ;)
RE: XML Reading/Parsing by Paril on 05-20-2008 at 01:26 PM

Oh, cool, this is much simpler than the code Josh gave me.. thanks

-Paril