Hello,
I have to do some java project in school and my teacher said that I have to use some API for XML-parsing. So that I have to have some source XML file.

I would like to make some little game like Pacman and I want that XML file to contain my level data (for example, positions of walls, coins, starting position, etc ...).

And my question is: Is this a good organizing of source XML file or should I make it in other way (for now, that XML file contains just positions of walls in level)?

Here is my source XML file:

<?xml version="1.0" encoding="utf-8"?>

<levels_data>
    <level1>
        <walls>
            <row1>
                <column1>1</column1>
                <column2>1</column2>
                <column3>1</column3>
                <column4>1</column4>
                <column5>1</column5>
                <column6>1</column6>
                <column7>1</column7>
                <column8>1</column8>
            </row1>
            <row2>
                <column1>1</column1>
                <column2>0</column2>
                <column3>0</column3>
                <column4>0</column4>
                <column5>0</column5>
                <column6>0</column6>
                <column7>0</column7>
                <column8>1</column8>
            </row2>
            <row3>
                <column1>1</column1>
                <column2>0</column2>
                <column3>1</column3>
                <column4>1</column4>
                <column5>0</column5>
                <column6>1</column6>
                <column7>1</column7>
                <column8>1</column8>
            </row3>
            <row4>
                <column1>1</column1>
                <column2>0</column2>
                <column3>1</column3>
                <column4>0</column4>
                <column5>0</column5>
                <column6>0</column6>
                <column7>1</column7>
                <column8>1</column8>
            </row4>
            <row5>
                <column1>1</column1>
                <column2>0</column2>
                <column3>1</column3>
                <column4>0</column4>
                <column5>1</column5>
                <column6>0</column6>
                <column7>0</column7>
                <column8>1</column8>
            </row5>
            <row6>
                <column1>1</column1>
                <column2>0</column2>
                <column3>1</column3>
                <column4>0</column4>
                <column5>1</column5>
                <column6>1</column6>
                <column7>0</column7>
                <column8>1</column8>
            </row6>
            <row7>
                <column1>1</column1>
                <column2>0</column2>
                <column3>0</column3>
                <column4>0</column4>
                <column5>0</column5>
                <column6>0</column6>
                <column7>0</column7>
                <column8>1</column8>
            </row7>
            <row8>
                <column1>1</column1>
                <column2>1</column2>
                <column3>1</column3>
                <column4>1</column4>
                <column5>1</column5>
                <column6>1</column6>
                <column7>1</column7>
                <column8>1</column8>
            </row8>
        </walls>
    </level1>
</levels_data>

Then I would use some XML parser (some tool from some Java API) and after that I loaded a level I would get something like this (now in Java code):

int walls[][] = {{1,1,1,1,1,1,1,1},
                 {1,0,0,0,0,0,0,1},
                 {1,0,1,1,0,1,1,1},
                 {1,0,1,0,0,0,1,1},
                 {1,0,1,0,1,0,0,1},
                 {1,0,1,0,1,1,0,1},
                 {1,0,0,0,0,0,0,1},
                 {1,1,1,1,1,1,1,1}};

P.S.: "1" means that "at this position is a wall" and "0" means that "at this position is not a wall". I could use "boolean" type in place of "int" and "true/false" in place of "1/0" (which would be more intuitive and logical) but because of simplicity I used just "zeros" and "ones".


Thanks.

i don't think it matters greatly how you set out your XML as long as you can parse it correctly. in saying that, i would probably avoid appending the numbers on "row" / "column" and instead just use the natural ordering to produce your level layout. if you do this, it will also mean your level is not tied down to a specific size.

This article has been dead for over six months. Start a new discussion instead.