JkWhoSaysNi
06-25-2007, 06:24 PM
To go with the Object -> XML conversion I posted a while back: http://forums.graalonline.com/forums/showthread.php?t=74337
function readXML(filename,object) {
this.xml = new TStaticVar();
this.xml.loadlines(filename);
this.stack = {};
while(this.xml.size() > 0) {
temp.tag = getTagName(this.xml[0]);
if (startTag(this.xml[0])) {
this.stack.insert(0,temp.tag);
//object.(@varStack()) = new TStaticVar();
varStack();
if (this.stack.size() == 1)this.roottag = temp.tag;
}
else if (endTag(this.xml[0])) {
this.stack.delete(0); //would be good here to check it's removing the correct end tag.
}
else {
if (temp.tag == "index")
makevar("object."@varStack()).add(this.xml[0].substring(this.xml[0].pos(">")+1,(this.xml[0].pos("</"))-(this.xml[0].pos(">")+(">").length())));
else {
vs = varStack();
if (vs == "")
object.(@temp.tag) = this.xml[0].substring(this.xml[0].pos("<"@[email protected]">")+("<"@[email protected]">").length(),(this.xml[0].pos("</"))-(this.xml[0].pos("<"@[email protected]">")+("<"@[email protected]">").length()));
else
makevar("object."@[email protected]"."@temp.tag) = this.xml[0].substring(this.xml[0].pos("<"@[email protected]">")+("<"@[email protected]">").length(),(this.xml[0].pos("</"))-(this.xml[0].pos("<"@[email protected]">")+("<"@[email protected]">").length()));
}
}
this.xml.delete(0);
}
}
function varStack() {
temp.res = "";
for(s: this.stack) {
if (s != this.roottag) {
if (temp.res == "") temp.res = s;
else temp.res = s @ "." @ temp.res;
}
}
return temp.res;
}
function endTag(line) {
temp.tag = getTagName(line);
return temp.tag.starts("/");
}
function startTag(line) {
temp.tag = getTagName(line);
if (temp.tag.starts("/")) return false;
else if (line.pos("<"@[email protected]">") >= 0 && line.pos("</"@[email protected]">") == -1) return true;
}
function getTagName(line) {
if (line.pos("<") >= 0 && line.charat(line.pos("<")+1) != '?') {
temp.open = line.pos("<");
temp.close = line.pos(">");
for (i = temp.open; i <= temp.close; i++) {
if (line.charat(i) == ' ') { temp.close = i; break; }
}
if ((temp.space < temp.close) && (temp.space > temp.open)) {
temp.close = temp.space;
}
return line.substring(temp.open+1,temp.close-temp.open-1);
}
}
usage:
readXML(xml file, object);
//e.g.
readXML("JkWhoSaysNi.xml",findplayer("JkWhoSaysNi"));
The object is sent to the function like that because for objects like player you can't do player = readXML(file) because player itself is not writable like that. It would be possible to have a more OO approach though using something like player.readXML(file); but do what you want with the code, it wouldn't be hard to change it.
There are some restrictions. It won't read just any XML file. The XML file has to:
Have start and end tags for objects on their own lines e.g.
//wont work:
<player><weapons>...</weapons></player>
//will work:
<player>
<weapons>
...
</weapons>
</player>
It would be possible to remove this restriction but it would require looping through the characters on each line and it would be much less efficient. This script is only really designed to import XML files that have been created using my exporter anyway.
There is also no error checking. An invalid XML file will get parsed incorrectly and not produce the object you want.
Enjoy!
-Knight
function readXML(filename,object) {
this.xml = new TStaticVar();
this.xml.loadlines(filename);
this.stack = {};
while(this.xml.size() > 0) {
temp.tag = getTagName(this.xml[0]);
if (startTag(this.xml[0])) {
this.stack.insert(0,temp.tag);
//object.(@varStack()) = new TStaticVar();
varStack();
if (this.stack.size() == 1)this.roottag = temp.tag;
}
else if (endTag(this.xml[0])) {
this.stack.delete(0); //would be good here to check it's removing the correct end tag.
}
else {
if (temp.tag == "index")
makevar("object."@varStack()).add(this.xml[0].substring(this.xml[0].pos(">")+1,(this.xml[0].pos("</"))-(this.xml[0].pos(">")+(">").length())));
else {
vs = varStack();
if (vs == "")
object.(@temp.tag) = this.xml[0].substring(this.xml[0].pos("<"@[email protected]">")+("<"@[email protected]">").length(),(this.xml[0].pos("</"))-(this.xml[0].pos("<"@[email protected]">")+("<"@[email protected]">").length()));
else
makevar("object."@[email protected]"."@temp.tag) = this.xml[0].substring(this.xml[0].pos("<"@[email protected]">")+("<"@[email protected]">").length(),(this.xml[0].pos("</"))-(this.xml[0].pos("<"@[email protected]">")+("<"@[email protected]">").length()));
}
}
this.xml.delete(0);
}
}
function varStack() {
temp.res = "";
for(s: this.stack) {
if (s != this.roottag) {
if (temp.res == "") temp.res = s;
else temp.res = s @ "." @ temp.res;
}
}
return temp.res;
}
function endTag(line) {
temp.tag = getTagName(line);
return temp.tag.starts("/");
}
function startTag(line) {
temp.tag = getTagName(line);
if (temp.tag.starts("/")) return false;
else if (line.pos("<"@[email protected]">") >= 0 && line.pos("</"@[email protected]">") == -1) return true;
}
function getTagName(line) {
if (line.pos("<") >= 0 && line.charat(line.pos("<")+1) != '?') {
temp.open = line.pos("<");
temp.close = line.pos(">");
for (i = temp.open; i <= temp.close; i++) {
if (line.charat(i) == ' ') { temp.close = i; break; }
}
if ((temp.space < temp.close) && (temp.space > temp.open)) {
temp.close = temp.space;
}
return line.substring(temp.open+1,temp.close-temp.open-1);
}
}
usage:
readXML(xml file, object);
//e.g.
readXML("JkWhoSaysNi.xml",findplayer("JkWhoSaysNi"));
The object is sent to the function like that because for objects like player you can't do player = readXML(file) because player itself is not writable like that. It would be possible to have a more OO approach though using something like player.readXML(file); but do what you want with the code, it wouldn't be hard to change it.
There are some restrictions. It won't read just any XML file. The XML file has to:
Have start and end tags for objects on their own lines e.g.
//wont work:
<player><weapons>...</weapons></player>
//will work:
<player>
<weapons>
...
</weapons>
</player>
It would be possible to remove this restriction but it would require looping through the characters on each line and it would be much less efficient. This script is only really designed to import XML files that have been created using my exporter anyway.
There is also no error checking. An invalid XML file will get parsed incorrectly and not produce the object you want.
Enjoy!
-Knight