• 10
• 12
• 12
• 14
• 15

Moving from plain "ascii" to XML?

This topic is 547 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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.

Share on other sites

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?

Share on other sites

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

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

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

std::ofstream fout("config.yaml");
fout << config;


Share on other sites

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

Share on other sites

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.

Share on other sites

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

Edited by TheChubu

Share on other sites
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... Edited by fastcall22