Trying to parse external files (XML specifically) to drive content

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By timbone316
:warning: Old Version Published before Godot 3 was released.

So I wanted to make a simple application. I like ot play fighting games (Mortal Kombat, Street Fighter, etc). I have found web pages and printables with movelists of how to do various moves for each character, so I decided to consolidate all of these into an app I could put on a phone or tablet so when i play the games, i can click on a game, click on a character, and be given their moves right before me. I was planning on it being a simple app. I have symbols for each of the inputs (directions, punches, kicks, etc.), and had made a scene that was a horizontal bar with a text label on it. My plan was to populate a scene with these bars, use the label to label the move, and then add sprites for each of the inputs (for example, in mortal kombat, Scorpion’s harpoon would have a text label with the text “Harpoon”, followed by a “back” sprite, a “back” sprite, and a “punch_low” sprite. I was planning on creating XMl files for each game, which would include inside of them the list of characters, and each character would have a list of moves, and each move would have a list of inputs. I am trying to get the XMLParser to work and canNOT figure it out, at all. Google searches are pointless, and the most help I’ve gotten is a generic reply that got me to use the .read() command after the .open() command, so now i can get to my root node, but can’t get to anything inside. Here is an XML snippet to begin to show what I was thinking of creating to accompany this:

<?xml version="1.0" encoding="UTF-8"?>
<chars>
	<char>
		<name>Liu Kang</name>
		<moves>
				<move>
						<name>Fireball</name>
						<inputs>
								<input>
										<type>Sprite</type>
										<label>fwd</label>
								</input>
								<input>
										<type>Sprite</type>
										<label>fwd</label>
								</input>
								<input>
										<type>Sprite</type>
										<label>punch_high</label>
								</input>
						</inputs>
				</move>
		</moves>
	</char>
</chars>

Using this code, i can get it to read the name “chars”

var data = XMLParser.new()
	data.open("res://assets/xmls/mk1.xml")
	data.read()
	data.skip_section()
	print(data.get_node_name())

and that’s all i can do. I have tried reading the node_data, seeing if i could get attributes, etc. and nothing. Is there a better way than XML? I have looked at json parsing, but am not familiar with json so figured it would be even more difficult. Can i do this in a plain text file and read it?

Sigh… this will be such an easy thing to develop once i get this part figured out. Its just a bunch of data entry and loading the right file, then i have to add either a sprite or a label to show what the inputs are. Any help at all is appreciated. Thanks so much guys!

:bust_in_silhouette: Reply From: jospic

There are </input> tags wrong. They are missed >

-j

Sorry, fixed it. This XML example was one i popped out real quick here to show what I would ultmately like to achieve, but i can’t get it to do anything. My first attempt was merely

<chars>
	<char>
		<name>Liu Kang</name>
		<moves>3</moves>
	</char>
</chars>

and i couldn’t even read the “char” element, let alone the “name” element.

timbone316 | 2016-09-28 14:33

I’d use json that is much more readable and modern:

json data version

{
  "chars": {
    "char": {
      "name": "Liu Kang",
      "moves": "3"
    }
  }
}

script json for reading

var file = File.new()
file.open("res://assets/json/mk1.json", File.READ)
var json_str = file.get_as_text()
var data = {}
data.parse_json(json_str)
print(data.chars.char.name)  # read your data NAME

-j

jospic | 2016-09-28 14:46

OK, that is progress, but how can i nest things inside? For instance, how would I get “Kano” from this json:"

{
  "chars": {
    "char": {
      "name": "Liu Kang",
      "moves": "3"
    }
    "char": {
      "name": "Kano",
      "moves": "3"
    }
    "char": {
      "name": "Scorpion",
      "moves": "3"
    }
  }
}

Or is that not even proper json formatting? Again, I have not done any json dev before. Thanks for your help, BTW!

timbone316 | 2016-09-28 14:54

Yes, formatting not even proper, this one should be right:

{   "chars": {
    "char": [{
      "name": "Liu Kang",
      "moves": "3"
    },
     {
      "name": "Kano",
      "moves": "3"
    },
    {
      "name": "Scorpion",
      "moves": "3"
    }]   } }

Regards
-j

jospic | 2016-09-28 15:00

Damn that is helpful! OK, LAST one… :confused:

I tried this, using what I thought was a logical progression:

{   
	"chars":{
		"char": [{
			"name": "Liu Kang",
			"moves": [{
				"name": "Fireball",
				"inputs": [{
					"type": "Sprite",
					"label": "fwd"
					},
					{
					"type": "Sprite",
					"label": "fwd"
					},
					{
					"type": "Sprite",
					"label": "punch_h"
					},
				}]
			}]
		  }]
		},
		 {
			"name": "Kano",
			"moves": "3"
		},
		{
			"name": "Scorpion",
			"moves": "3"
		}]
	}
}

but when i say "print(data.chars.char[0].moves[0].name) it gives me an error. Is that too much nesting? If so, is json not a good choice?

timbone316 | 2016-09-28 15:26

Sorry but your last json data example is not well format…for this reason causes error.
Please type the xml version so I understand the correct hierarchy

-j

jospic | 2016-09-28 15:44

Maybe if i explain the structure I was attempting, that will help (thanks again for your help, by the way).

I was going to have a dictionary of characters with a “name” key (Liu Kang, Ryu, etc), and a “moves” dictionary as a a value. Each “moves” dictionary would have a “name” key (Fireball, Uppercut, etc) and an “inputs” array as a value. Each “inputs” array would have a list of the motions required to carry out the move, in order (“forward”, “forward”, “high punch”). So, to access, for instance, the sprites required to show Liu Kang’s Fireball, I might try to access chars.char[0].moves[0].inputs, and to access the name, I might try to access chars.char[0].moves[0].name. Does that make any sense?

timbone316 | 2016-09-28 17:33

Nevermind, I got it to work. I could NOT have done it without your help. Thank you so much!

timbone316 | 2016-09-28 18:42