Whats the prefered approach to reading xml config files within jmonkey?

Whats the prefered approach to reading xml config files within jmonkey?

I used plain Java DOM at the beginning but it became quickly a hassle after my application got bigger. I switched then to JAXB since it does a lot of stuff automatically. But it can take some time to modify it to your needs. Still i don’t regret it. I barely have to do work now in my xml section.

1 Like

At any instance you should create a loader for your file format, and I suggest not using an .xml ending as file types are recognized by suffix in the assetManager. This will allow you to use the caching and locating functions of the assetManager instead of re-implementing the actual file access each time you move to a new platform or the config files to another place (e.g. On the classpath, in a jar, in a folder, etc.)

And really just curious… but is there a reason you picked XML over something like properties files or JSON?



Is this just for application config storage (app saves it app loads it) or do you expect users to hand edit the files, too? If the first case, do you expect them to be able to backup and move the files around on their own or would Java’s preferences API work?

The idea is to define my levels in xml and then build each level based on this. This will include loading actors landscape powerups etc. I want to build a level editor.

The SDK is a level editor for you to use already. It will be easier just build a plugin for it that can talk to your specific game domain objects than building your own editor from scratch. Also defining where spatials live in a 3d world using xml will take ages. Unless your levels are entirely different than a standard game level, then I cannot recommend what to use.

Check out what I did here: http://hub.jmonkeyengine.org/groups/free-announcements/forum/topic/powermonkey/#post-159693

Sorry to bump an old post but since this thread is the first result to come up on Google when searching “jme xml” or “jmonkeyengine xml”, I thought I’d share my current approach to parsing XML files. I’m sure this can be useful for anyone else reaching this page via Google. I followed normen’s recommendation of creating a custom AssetLoader.

My problem:
For my game I load and index all meshes at startup, so I can create Geometry nodes with those meshes later on. I want to maintain the list of mesh.xml files outside of the code in a separate xml file. Why keep the list not in code? Because I’m working together with non-programmers.

Solution:
Create an AssetLoader that parses the asset’s input stream as an XML stream.
[java]
public class MyXMLLoader implements AssetLoader {

public Object load(AssetInfo assetInfo) {
    Document doc = createDocFromStream(assetInfo.openStream());
    if (doc != null) {
        // Process document here
        // doc.getElementsByTagName("...")
    }

    // Return an instance of your result class X here
    return new Object();
}

public static Document createDocFromStream(InputStream inputStream) {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(inputStream);
        return doc;
    } catch (Exception ex) {
        return null;
    }
}

}
[/java]

Register your asset loader to your custom “myext.xml” filetype.
[java]
public void simpleInitApp() {
assetManager.registerLoader(MyXMLLoader.class, “myext.xml”);

    // Type cast to your result class X here
    Object result = assetManager.loadAsset("myXmlFile.myext.xml");
}

[/java]

In this example, I have a registered the file extension *.myext.xml to my asset loader. Hope this is useful.

1 Like