a firstworks project
Rudiments
About Documentation Download Licensing News

Using the propdom class

The propdom class provides a framework for DOM parsing of Java-style properties files. It parses a file or string of property-file-formatted data and produces a dom tree representing the data. Since the propdom class creates a representation of the properties data in memory, it should not be used to process arbitrarily large files which could exhaust system memory.

The dom tree uses "k" elements for keys, "p" elements for pound-delimited comments, and "e" elements for exclamation-delimited comments. Each element has a "v" attribute containing the value. Each "k" element also has a "k" attribute containing the key name.

The following program parses a properties string containing a list of animals mapped to their types, walks the tree to print each key-value pair, and writes the properties data back to standard output.

#include <rudiments/propdom.h>
#include <rudiments/domnode.h>
#include <rudiments/stdio.h>

int main(int argc, const char **argv) {

	propdom	prop;

	// parse a properties string
	prop.parseString(
		"# animal types\n"
		"dog=mammal\n"
		"eagle=bird\n"
		"snake=reptile\n");


	// walk the tree
	// the dom tree structure is:
	//   root -> k (key, with "k" and "v" attributes)
	//           p (pound comment, with "v" attribute)
	domnode	*root=prop.getRootNode();

	stdoutput.write("all animals:\n");
	for (domnode *node=root->getFirstTagChild()->
					getFirstTagChild("k");
			!node->isNullNode();
			node=node->getNextTagSibling("k")) {

		const char	*name=node->getAttributeValue("k");
		const char	*type=node->getAttributeValue("v");

		stdoutput.printf("  %s = %s\n",name,type);
	}
	stdoutput.write('\n');


	// write the properties tree to standard output
	stdoutput.write("properties output:\n");
	prop.write();
}
Copyright 2017 - David Muse - Contact