XML Reading/Parsing |
Author: |
Message: |
Paril
Junior Member
Admin of Paril's Projects
Posts: 69
31 / /
Joined: Jul 2006
|
O.P. XML Reading/Parsing
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
|
|
05-18-2008 01:48 PM |
|
|
mynetx
Skinning Contest Winner
Microsoft insider
Posts: 1175 Reputation: 33
37 / /
Joined: Jul 2007
|
|
05-18-2008 02:01 PM |
|
|
Paril
Junior Member
Admin of Paril's Projects
Posts: 69
31 / /
Joined: Jul 2006
|
O.P. RE: XML Reading/Parsing
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
|
|
05-18-2008 02:04 PM |
|
|
Paril
Junior Member
Admin of Paril's Projects
Posts: 69
31 / /
Joined: Jul 2006
|
O.P. RE: XML Reading/Parsing
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
This post was edited on 05-18-2008 at 02:17 PM by Paril.
|
|
05-18-2008 02:17 PM |
|
|
Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
Joined: Apr 2004
|
RE: XML Reading/Parsing
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();
|
|
05-18-2008 04:46 PM |
|
|
Paril
Junior Member
Admin of Paril's Projects
Posts: 69
31 / /
Joined: Jul 2006
|
O.P. RE: XML Reading/Parsing
Ahh, that's perfect. Thanks. My uber-ness can take over from here.
-Paril
|
|
05-18-2008 09:59 PM |
|
|
markee
Veteran Member
Posts: 1622 Reputation: 50
36 / /
Joined: Jan 2006
|
|
05-18-2008 10:40 PM |
|
|
Paril
Junior Member
Admin of Paril's Projects
Posts: 69
31 / /
Joined: Jul 2006
|
O.P. RE: XML Reading/Parsing
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
This post was edited on 05-19-2008 at 12:15 AM by Paril.
|
|
05-19-2008 12:14 AM |
|
|
Matti
Elite Member
Script Developer and Helper
Posts: 1646 Reputation: 39
32 / /
Joined: Apr 2004
|
RE: XML Reading/Parsing
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.
|
|
05-19-2008 03:36 PM |
|
|
Paril
Junior Member
Admin of Paril's Projects
Posts: 69
31 / /
Joined: Jul 2006
|
O.P. RE: XML Reading/Parsing
Oh, cool, this is much simpler than the code Josh gave me.. thanks
-Paril
|
|
05-20-2008 01:26 PM |
|
|
|