An archive of boards.dungeonrunners.com (2006–2010), recovered from the Wayback Machine.

XML Character Data » Tutorial: chardata XML in javascript View all threads
 
MythicalJeff
New Kid
Posts: 22
Tutorial: chardata XML in javascript
#46190 - 02/03/08 09:09 AM

Full page:
Quote:

Parsing DR XML into javascript arrays




Excerpt:
Quote:


This Might Be All You Really Want

DRxml2arrays.js

There’s all the javascript required to parse a Dungeon Runners’ XML file into javascript arrays.




I hope someone finds this of use...


--------------------
Add MyDungeonRunner to your blog.



Snarky
Chicken Hawk
Posts: 108
Re: Tutorial: chardata XML in javascript
#46194 - 02/03/08 09:28 AM

I love that of the char viewers I know of, they're all done a different way. Demos uses XLS, You've got Javascript, I'm in php, and there's an ASP one floating around as well.

/me thinks we need a community page with explanations of how it works and basic examples for each language.

--------------------
--Snarky (aka Segfault)
--Three Planets Software



MythicalJeff
New Kid
Posts: 22
Re: Tutorial: chardata XML in javascript [Re: #46194]
#46270 - 02/04/08 12:31 AM

Quote:

I love that of the char viewers I know of, they're all done a different way. Demos uses XLS, You've got Javascript, I'm in php, and there's an ASP one floating around as well.

/me thinks we need a community page with explanations of how it works and basic examples for each language.




That'd be awesome.

I really tried to provide the basic (usable) code as well, so anyone working in javascript could get right to doing whatever they are wanting to do.

Though it's a bit shy of being an actual library - I think providing open source libraries for different languages would really be ideal (in addition to explanations and examples).

Plus there's some high-level info about the feed data itself that would useful regardless of what language a person is working with... thinking of things like which item slot number is which item slot, what the quality values correspond to in terms of item rarity and colors, etc.

Not that it was real hard to figure that stuff out, but it was 30 to 45 minutes of time that we could save the next person.

Maybe a page on http://www.thetownstons.com/?


--------------------
Add MyDungeonRunner to your blog.



Demosthenez
Rabbit Puncher
Posts: 72
Re: Tutorial: chardata XML in javascript [Re: #46270]
#46283 - 02/04/08 01:24 AM

A link to each site from the townstons as well as an html code block for each source file would be a good start.


Snarky
Chicken Hawk
Posts: 108
Re: Tutorial: chardata XML in javascript [Re: #46283]
#46289 - 02/04/08 06:21 AM

I'm asking Crickette for a board on here, as I think it'd be best for the information to be very easy to find (not knocking TheTownstons here, that'd be a good backup).

I've been pondering just what'll be needed for a php library since then it won't just be saving someone 30-45 minutes, but allow anyone with even very basic php knowledge to put one together.

--------------------
--Snarky (aka Segfault)
--Three Planets Software



Legendrunner
Fully Decked out Skittle
Posts: 1,442
Re: Tutorial: chardata XML in javascript [Re: #46289]
#46294 - 02/04/08 09:03 AM

I dont know exactly what you people want, but as long as its a good idea, Im up for it, just send me a PM with as many info as you want/requests, and Ill look what I can do

--------------------
LegendRunner - level 100 Ace something

The Townstons Wiki Archive! - Boss of TT



Snarky
Chicken Hawk
Posts: 108
Re: Tutorial: chardata XML in javascript [Re: #46294]
#46323 - 02/04/08 04:54 PM

We're getting a board on these forums, Crickette confirmed that to me today. While we can copy stuff over to TheTownstons if we want, lets build a good background on here first.

--------------------
--Snarky (aka Segfault)
--Three Planets Software



WiFiHi
Armchair Warrior
Posts: 40
Re: Tutorial: chardata XML in javascript
#46351 - 02/04/08 07:36 PM

Nice work. What would be cool is if the DR devs would provide an alternate output in json. It sure would make consuming the data easier than parsing xml.

--------------------
DR Old Timer
www.drplayers.com



MythicalJeff
New Kid
Posts: 22
Re: Tutorial: chardata XML in javascript [Re: #46351]
#48353 - 02/17/08 06:54 AM

Quote:

Nice work. What would be cool is if the DR devs would provide an alternate output in json. It sure would make consuming the data easier than parsing xml.




Ta da!

http://mythicalblog.com/dungeonrunners/json/?name=$CHARNAME

Unfortunately, I am now sleepy, and I haven't actually checked-up on the data being there and right and so on.

And that was a lot harder with php4 than it would have been with php5.

Heh.


--------------------
Add MyDungeonRunner to your blog.



MythicalJeff
New Kid
Posts: 22
Re: Tutorial: chardata XML in javascript [Re: #48353]
#48533 - 02/18/08 12:08 PM

Ugh.

Ok... NOW.

--------------------
Add MyDungeonRunner to your blog.



MythicalJeff
New Kid
Posts: 22
Re: Tutorial: chardata XML in javascript [Re: #48533]
#48630 - 02/19/08 06:52 AM

C'mon... that's impressive.

I demand a kudo!




--------------------
Add MyDungeonRunner to your blog.



Ripper_McGee
Karl's "bad cavern" spelunker
Posts: 2,360
Re: Tutorial: chardata XML in javascript [Re: #48630]
#48637 - 02/19/08 09:53 AM

That's good stuff, Jeff. Now, if only I had a blog to which I could add a gadget!

Seriously, excellent work!

--------------------
Bring out your widgets!

My ideas!

/twitch ™



thysam
New Kid
Posts: 20
Re: Tutorial: chardata XML in javascript [Re: #48630]
#48642 - 02/19/08 11:31 AM

Kudos! Now I just need to learn how to use it so I can get this working.

--------------------
ThySamurai
RighteousStomp



Theck
Rabbit Puncher
Posts: 86
Re: Tutorial: chardata XML in javascript [Re: #48630]
#50858 - 03/11/08 08:49 PM

Hey there MythicalJeff,

I have been trying for a week now to get your Javascript DRXML library working, and have had no success still.

I copy and pasted the script verbatim, and added in the function that you posted to load the XML data right under the xmldoc variable. I have called the loadXML function and passed it the url, yet it still will not work. What am I doing wrong?

I'd really appreciate it if you took a look at this, because I am not a complete whiz at this type of scripting, and of course Javascript doesn't tell you what you're doing wrong - it just stops working. :P

Thanks!

Code:

<script type="text/javascript">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
function loadXML(xmlFile) {
xmlDoc.async="false";
xmlDoc.onreadystatechange=verify;
xmlDoc.load(xmlFile);
ticker=xmlDoc.documentElement;
DRxml2arrays( xmlDoc )
}
function verify() {
return (xmlDoc.readyState == 4)
}
}
/***********************************************************
DRxml2arrays() - javascript for converting Dungeon Runners
character data in XML format to javascript arrays.

Permission is granted for you to do whatever you want to do.
If you find this useful, thank the internet for it.
**********************************************************/


/***********************************************************
Define these arrays and consts with the appropriate scope.
They are not passed to DRxml2arrays - it just expects
they are accessible.
**********************************************************/

var charData = new Array();
/***********************************************************
charData values are:

charData['Name']
charData['Gender']
charData['Level']
charData['Title']
charData['PVPWins']
charData['Appearance']['Face']
charData['Appearance']['Hair']
charData['Appearance']['HairColor']
charData['Attributes'[Strength']['Innate']
charData['Attributes'[Strength']['Effective']
charData['Attributes'[<Agility>']['Innate']
charData['Attributes'[<Agility>']['Effective']
...and so on
View the XML for the complete list
**********************************************************/

var skillData = new Array();
/***********************************************************
SkillData values are in skillData[ SLOT_ID ]
SlotID's for skills are 100 through 109
Good way to access them:
for(key in skillData) {
var strName = skillData[key]['Name'];
var strIcon = skillData[key]['Icon'];
var strLevel = skillData[key]['Level'];
do_stuff...
}
otherwise, if no skill is in a slot, there will be no index for it
if (skillData[ SLOT_ID] ) {
do_stuff...
} else {
//there is no skill in this slot
}
/**********************************************************/

var itemData = new Array();
/***********************************************************
Replace $ITEM_CONST$ with the appropriate item const
from below. e.g. for the necklace, ITEM_NECK, etc.

itemData[ $ITEM_CONST$] ['Name']
itemData[ $ITEM_CONST$] ['Level]
itemData[ $ITEM_CONST$] ['Quality'] - see below for quality consts
itemData[ $ITEM_CONST$] ['SoulBound']
itemData[ $ITEM_CONST$] ['Icon']
itemData[ $ITEM_CONST$] ['Mod'][ 0 ..1 .. 2 .. 10 ]

If there's no item in a given slot, there will be no entry in the array
with that key. Always check:
if (itemData[$ITEM_CONST$] ) {
do_stuff...
};
************************************************************/
const ITEM_NECK = '1';
const ITEM_BRACER = '2';
const ITEM_LEFT_RING = '3';
const ITEM_RIGHT_RING = '4';
const ITEM_HELM = '5';
const ITEM_TORSO = '6';
const ITEM_BOOT = '7';
const ITEM_SHOULDER = '8';
const ITEM_OFF_HAND = '9';
const ITEM_PRIMARY_HAND = '10';

/***********************************************************
itemData[ $ITEM_CONST$] ['Quality'] is a number from 1 to 7
These consts are arrays from 0 to 7.
e.g.
a quality of 1 is a "Normal" or "grey" item.
a quality of 3 is a "Magic" or "blue" item.
**********************************************************/
const QUALITY = [ "white", "grey", "green", "blue", "yellow", "purple", "sky", "rainbow" ];
const QUALITY_COLOR = [ "#FFFFFF", "#736F6E", "#00FF00", "#306EFF", "#FFFF00", "#D462FF", "#A0CFEC", "#FFCCFF" ];
const QUALITY_TITLE = [ "trash", "Normal", "Superior", "Magic", "Rare", "Unique", "Quest", "Mythical" ];

var attackData = new Array();
/***********************************************************
primary and secondary weapon data will be in
attackData['primary'] and attackData['offhand']

attackData[$ATTACK_NUM$] ['AttackRating']
attackData[$ATTACK_NUM$] ['MinDamage']
attackData[$ATTACK_NUM$] ['DamageMax'] <-- Not 'MaxDamage' like a normal person would use
attackData[$ATTACK_NUM$] ['AttackSpeed']

If there's no offhand weapon, it'll still be loaded with zeros.
This means that " if ( attackData[ATTACK_SECONDARY] ) will
always return true!
************************************************************/
const ATTACK_PRIMARY = 'primary';
const ATTACK_SECONDARY = 'offhand';

/***********************************************************
Pass the XML data from
http://www.dungeonrunners.com/characters/CHARACTER_NAME
in the DOM object you get back.
Notice the lack of data validation here. Give it good stuff.
************************************************************/
function DRxml2arrays( XMLdata ) {

charData = xml2array(XMLdata);
charData = charData['DRCharacter'];

for ( var index = 0; index < 2; index++ )
attackData[ charData['Combat']['Weapon'][index]['hand'] ] = charData['Combat']['Weapon'][index];

for ( var index = 0; index < 10; index++ ) {
if ( charData['Equipment']['Item'][index] )
itemData[ charData['Equipment']['Item'][index]['SlotID'] ] = charData['Equipment']['Item'][index];
};

for ( var index = 0; index < 10; index++ ) {
if ( charData['Skills']['Skill'][index] )
skillData[ charData['Skills']['Skill'][index]['SlotID'] ] = charData['Skills']['Skill'][index];
};

var not_whitespace = new RegExp(/[^\s]/);
var parent_count;

function xml2array(xmlDoc,parent_count) {

function addAttributesToArray( attribArray ) {
if(xmlDoc.childNodes[i].attributes.length) {
for(var j=0; j<xmlDoc.childNodes[i].attributes.length; j++) {
var nname = xmlDoc.childNodes[i].attributes[j].nodeName;
if(nname) {
attribArray[nname] = xmlDoc.childNodes[i].attributes[j].nodeValue;
}
}
}
return attribArray;
}



var arr;
var parent = "";
parent_count = parent_count || new Object;

if(xmlDoc.nodeName && xmlDoc.nodeName.charAt(0) != "#") {
if(xmlDoc.childNodes.length > 1) {
arr = new Object;
parent = xmlDoc.nodeName;

}
}
var value = xmlDoc.nodeValue;
if(xmlDoc.parentNode && xmlDoc.parentNode.nodeName && value) {
if(not_whitespace.test(value)) {
arr = new Object;
arr[xmlDoc.parentNode.nodeName] = value;
}
}

if(xmlDoc.childNodes.length) {
if(xmlDoc.childNodes.length == 1) { //Just one item in this tag.
arr = xml2array(xmlDoc.childNodes[0],parent_count); //:RECURSION:
} else { //If there is more than one childNodes, go thru them one by one and get their results.
var index = 0;

for(var i=0; i<xmlDoc.childNodes.length; i++) {//Go thru all the child nodes.
var temp = xml2array(xmlDoc.childNodes[i],parent_count); //:RECURSION:
if(temp) {
var assoc = false;
var arr_count = 0;
for(key in temp) {
if(isNaN(key)) assoc = true;
arr_count++;
if(arr_count>2) break;//We just need to know wether it is a single value array or not
}

if(assoc && arr_count == 1) {
if(arr[key]) {
if(!parent_count || !parent_count[key]) {
parent_count[key] = 0;

var temp_arr = arr[key];
arr[key] = new Object;
arr[key][0] = temp_arr;
}
parent_count[key]++;

arr[key][parent_count[key]] = addAttributesToArray( temp[key] );
} else {
parent_count[key] = 0;

arr[key] = addAttributesToArray( temp[key] );
}
} else {

arr[index] = addAttributesToArray( temp );
index++;
}
}
}
}
}

if(parent && arr) {
var temp = arr;
arr = new Object;

arr[parent] = temp;
}
return arr;
}
}
</script>



--------------------
"The ability to speak does not make you intelligent."
  • Theck - 37 Ranger
  • Alkarius - 30 Fighter
  • Atalar - 16 Mage
Co-leader of Valor!



Blunder
New Kid
Posts: 1
Re: Tutorial: chardata XML in javascript [Re: #50858]
#57868 - 05/09/08 07:21 PM

Hey Theck,

I don't have much to go with to help you, except that you CAN get JavaScript to help with errors- wrap your code with a try...catch block, almost just like in Java.
Code:
 
try{
doSomeCode();
} catch (e){
alert("You messed up: " + e.message);
}


That's all there is to it. I hope this helps some.

Edited by Blunder (05/09/08 07:23 PM)


Theck
Rabbit Puncher
Posts: 86
Re: Tutorial: chardata XML in javascript [Re: #57868]
#57875 - 05/09/08 08:31 PM

Thanks mate, that'll help - I eventually just gave up on using Javascript for this, and am now just using PHP since I find that alot easier to program in. I appreciate it though!

--------------------
"The ability to speak does not make you intelligent."
  • Theck - 37 Ranger
  • Alkarius - 30 Fighter
  • Atalar - 16 Mage
Co-leader of Valor!



 
All times are US Eastern, the board's original clock (vBulletin-era times converted from GMT).
Jump to