I should begin by stating that XML and I don't seem to get along very well. I've just about finished the interface on the level editor for a game I've made, and the next step is generating a XML file so that it can be read into the game dynamically.

The problem is, well, the XML. I've been fighting it for the past hour or two, and still can't achieve what I would like.

This may not be valid XML, but it is a good idea of what I would like to achieve:

<root>
    <containers>
        <container>
            <name>Unsorted Objects</name>
            <children>
                <object>
                    <name>Img1 - container 1</name>
                    <x>420</x>
                </object>
                <object>
                    <name>Img2 - container 1</name>
                    <x>902</x>
                </object>
            </children>
        </container>
        <container>
            <name>Special Objects</name>
            <children>
                <object>
                    <name>Spcl1 - container 2</name>
                    <x>420</x>
                </object>
                <object>
                    <name>Spcl2 - container 2</name>
                    <x>902</x>
                </object>
            </children>
        </container>
    </containers>
</root>

Anyways, what libraries would you all suggest to write XML like this and then, later, parse it? I've tried ElementTree, and I seem to like it, but I just can't do what I want to. Sure, those examples with two objects each with two properties are nice and pretty, but none had the same level of depth as my XML shown above, leaving me confused. When I try to extend them, I keep getting None as the return value for my calls.

If somebody could just give me a little code sample explaining on how to parse/write XML at this level of depth, I would really appreciate it.

Thanks so much in advance.

Recommended Answers

All 8 Replies

And who is forcing you to use XML? It is generally considered more as part of the problem rather than solution.

Sometimes it's appropriate to use XML.

You should consider looking at the xml tutorial in Dive Into Python.

Thanks for the relies. I have actually tried the dive into python tutorial, and that's when I got all of those None return types.

Tonyjv, what would you recommend using instead of xml? The hierarchy is very important, but if you have a suggestion that would work, please do tell.

By the way, I'm going to have to generate the "code" just in python, but I will have to parse the information in both python and java.

Thanks again.

About conflict of XML way and Python check this famous link, third dot: http://dirtsimple.org/2004/12/python-is-not-java.html

I have impression that reStructuredText could be nice, though I have not used it myself: http://docutils.sourceforge.net/rst.html Or maybe ConfigParser is more for your use case: http://docs.python.org/library/configparser.html

Many cases it is OK to just pickle Python data to disk and unpickle it to retrieve it.

I have to say, I loved that article you linked me to. The reason I'm writing this level editor in Python is just because I much prefer it to Java. The only reason I'm writing my game in Java is because its for the Android platform.

Anyways, that article inspired me to not use xml, so I'll either check out configparser or just use a database.

Question answered. Thanks.

I still standby the fact that XML is a great way to store initialization data. Good example of that is glade.

I thought I would post a follow-up to this question, to hopefully provide useful information for future Android developers.

After I marked this thread as solved, I chose to use a sqlite database to store the information for each of the levels in my game. This was very easy to save/load in Python, and I expect the same is true for normal Java, but Android is a different story.

From the Android API, it is impossible to simply place any database in your "assets" folder and load it from your app. Instead, you have to add a special table for Android meta information and add a specially named "_id" column. After this, you have to copy your existing database byte-by-byte to the system database path, and then finally you can work with it.

I personally thought this was way too much work for just loading an external database, so I now somewhat regret using databases. I think I will end up writing my own minimalist text parsing system that is centered around what I truly need.

Hope this proves to be useful to somebody.

After I marked this thread as solved, I chose to use a sqlite database to store the information for each of the levels in my game. This was very easy to save/load in Python, and I expect the same is true for normal Java, but Android is a different story.

Just like to point out that this is exactly what the TAG Engine uses.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.