Moving from plain "ascii" to XML?

Started by
17 comments, last by cozzie 7 years, 6 months ago
Hi all,
I'm planning to move from reading ascii/ plain text from files, to XML.
To explain myself better, my 3d engine reads data from files, just an example:

File: settings.dat
Contents:
#XRESOLUTION 1280
#YRESOLUTION 720
#WINDOWED false
Etc.

In my codebase I simply use an fstream and read out the values linearly through the file, checking the labels.
Now I want to change this to XML, example:

<d3d11_settings>
<xresolution>1280</xresolution>
<yresolution>720<yresolution>
<windowed>true</windowed>
</d3d11_settings>

My questions are:
- what tools can I use to generate/ manually create the XSD? (defining field names, minoccurs/maxoccurs, data type etc.)
- assuming I have the XSD, what (parser) library do you advice?

I'm trying to keep things simple, for example the library should simply return me the xresolution value when I request it (from the loaded XML file).
Any input is appreciated.

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Advertisement

Keeping things simple would be not using a XSD at all. What is it good for, really? You use the XML to store game asset data or game settings, right? So it either goes through a tool in your asset pipeline which will detect whether everything is good, and in the other case the XML is program-generated anyway (so it had better be correct!). You're not writing an academic paper, are you.

Note that parsers which respect XSDs are about 10x larger libraries and much slower, too.

I've used TinyXML in the past, which works nice. There is an in-place parser made by some Russian guy which is much faster too (forgot the name!)... used that also, works fine.

But... do you really want to use XML? It is one of these things that somehow at some point gained huge popularity and nobody can explain why (I guess because Java used it for RPC?) but that is actually not all that great, and a lot of people who are or have been using it ask themselves: How could I ever do that? (I'm asking myself that)

Pretty much everything else is better than XML. Consider JSON if nothing else, or even invent your own data description language (it doesn't need to be able to do everything, you know... it just has to work, and be unambiguous).

I'm planning to move from reading ascii/ plain text from files, to XML.
...
I'm trying to keep things simple,

You have simple. Moving to XML buys you what exactly? Bloated and less readable data files? Slower parsing? Dependencies on third party XML parsers?

http://yaml.org/ , in particular, yaml-cpp. From their example/tutorial:


YAML::Node config = YAML::LoadFile("config.yaml");

if (config["lastLogin"]) {
  std::cout << "Last logged in: " << config["lastLogin"].as<DateTime>() << "\n";
}

const std::string username = config["username"].as<std::string>();
const std::string password = config["password"].as<std::string>();
login(username, password);
config["lastLogin"] = getCurrentDateTime();

std::ofstream fout("config.yaml");
fout << config;
RIP GameDev.net: launched 2 unusably-broken forum engines in as many years, and now has ceased operating as a forum at all, happy to remain naught but an advertising platform with an attached social media presense, headed by a staff who by their own admission have no idea what their userbase wants or expects.Here's to the good times; shame they exist in the past.

As has been said before the current format is already much simpler than XML... I wouldn't bother with XML unless you end up with complex enough structures to warrant deep trees. Doesn't look like that's the case here.

If you really want to switch to something more standard, I guess you could go with INI files, though there's a bunch of libraries out there to look for... and it may even be feasible to just write your own parser (the main reason they took off back then is that they're that simple, after all). Or even modify your own, I mean, replace the space with an equals sign and you're halfway there =P

Don't pay much attention to "the hedgehog" in my nick, it's just because "Sik" was already taken =/ By the way, Sik is pronounced like seek, not like sick.

Honestly, XML is pretty verbose and doesn't carry its weight in simple scenarios where flat files, ASCII, JSON, or Yaml would do just as well. XML is an awesome thing (with its share of flaws as well), but it really only shines when you need to represent complex, arbitrarily-nested information in a structured hierarchy, and it only comes into its own when that structure is formally specified, verified, and transformed through the XML ecosystem.

If what you really need is just a list of variables, flat files or ascii text files are just fine. If you need arbitrarily-nested information -- but don't need formal structure -- JSON or Yaml are perfect; in fact, they're great options when you explicitly *don't want* a formal structure -- many times the lack of strict structure is a benefit, all JSON and Yaml structure is ad-hoc. Only when you need a formally specified, hierarchical structure that can be verified and transformed is XML necessary. XML might be sometimes beneficial for interop scenarios as well, or certain encoding scenarios, but JSON is usually just as good unless you're tied to some (rare) party that understands XML but not JSON.

throw table_exception("(? ???)? ? ???");

Note that parsers which respect XSDs are about 10x larger libraries and much slower, too.

And on this point, note that you probably don't need the functionality that XSD provides at runtime in any case. XSD is useful for validation, and for authoring content in aware text editors, and for enabling XSLT transformations, but you don't need any of that after release. Your XML files will already have been authored and transformed and ought already have been validated by the time you ship them to customers.

throw table_exception("(? ???)? ? ???");

Lets see, verbose XML:


<d3d11_settings>
  <xresolution>1280</xresolution>
  <yresolution>720<yresolution>
  <windowed>true</windowed>
</d3d11_settings>

If you're doing XML you should probably do it like:


<d3d11_settings
 xresolution="1280"
 yresolution="720"
 windowed="true" />

ie, use attributes.

If you're doing JSON:


"d3d11_settings": {
  "xresolution": 1280,
  "yresolution": 720,
  "windowed": true
}

ie, nicer.

If you're doing YAML:


d3d11_settings:
  xresolution: 1280
  yresolution: 720
  windowed: true

ie, "look ma! no double quotes!".

I'd use something else than XML, YAML is nice. JSON is not as nice, but its much more widely used (thus more info on it, more libs, etc).

EDIT: Fixed the JSON as fastcall mentions below.

Anyway, this is my YAML window stuff as an example:


width: 1280
height: 720
fullscreen: false
vsync: yes
softsync: false
vfov: 75
renderdist: 256
resizable: yes

Basically using it as a normal key-value thingy, except i can just call "load" and I get a Map<String,Object> out of it. My renderer pipeline is also made in YAML, using the compact notation, here is a render pass:


dirlight_pass: &dirlight_pass
  name: dirlight_pass
  program: df_dir_light
  drawing:
    name: gbuffer
    targets: [light_acc]
  sampling:
    name: gbuffer
    targets: [albedo, normal, misc, depth]
  samplers:
    - [default, default, default, default]
  batch: LIGHT_DIR
  updaters: [DIRLIGHT]
  descriptors:
    writing: COLOR_ONLY
    depth:
      func: GREATER
      range: [1.0, 1.0]
      testing: YES
      clamping: NO
    scissor: DEFAULT
    viewport: DEFAULT
    stencil: NO
    triangle: DEFAULT
    blending: NO
    clearing: COLOR_ONLY

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

Chubu’s JSON example is not quite correct. There are other datatypes besides string in JSON:
{
    "d3d11_settings": {
        "xresolution": 1280,
        "yresolution" 720,
        "windowed": true
    }
}
Though, I would probably combine the resolution properties into an array:

"resolution": [1280,720],



Call me old fashioned, but a simple `key=value` with `#comments` is dead simple and suitable for most configuration. INI, perhaps...
Another option would be using Lua. It's a full blown scripting language, that actually started out as being used for config files.

If you intend to add scripting support to your library as well this might be a good match.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

This topic is closed to new replies.

Advertisement