C++ XML Objects Use: Advanced stuff

Previous Next Usage Home Installation Docs

Abstract

Here are some advanced things you can do with the XML file.

Naming Objects

In our original file, the objects were unnamed, but they can be named like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<root name="root">
	<x>1</x>	
	<foos>
		<foo name="foo_1">
			<x>1</x>
			<y>2.1</y>
			<z/>
		</foo>
	</foos>
</root>

The names are used when searching for objects, and also when trying to navigate objects. The original XML file would have created "dummy" names for the objects like "_0", "_1" etc.

Importing

You can place any object in another XML file, and include parameters which are then defined when importing from another file. The Parameters are simply textual replacements while importing. Importing acts exactly like you had placed the same object inline.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<root>
	<x>1</x>	
	<foos>
		<import url="foo.xml" name="foo_1">
			<param name="x" value="1"/>
			<param name="y" value="1.2"/>
			<param name="z" value="true"/>
		</import>
		<import url="foo.xml" name="foo_2">
			<param name="x" value="4"/>
			<param name="y" value="10"/>
			<param name="z" value="false"/>
		</import>
	</foos>
</root>

And in a file called "foo.xml":

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<foo>
	<x>$(x)</x>
	<y>$(y)</y>
	<z>$(z)</z>
</foo>

You can also replace:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<root>
	<x>1</x>	
	<foos>
		<import url="foo.xml"/>
	</foos>
</root>

with:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<root xmlns:xi="http://www.w3.org/2001/XInclude">
	<x>1</x>	
	<foos>
		<xi:include href="foo.xml"/>
	</foos>
</root>

Which will allow your XML files to be used by XSL. But an important thing about this is that you can't pass parameters to these files, since that wouldn't match the XInclude capability. In fact there is nothing preventing from doing it, but down the track your XSL might fail because your not evaluating all of the parameters.

Fragments

Fragments allow objects to be declared anywhere within the outer XML file object, but they place the object declared inside the fragment onto an arbitrary place in the object tree.

Consider this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<root>
	<x>1</x>	
	<foos>
		<import url="foo.xml" name="foo_1">
			<param name="x" value="1"/>
			<param name="y" value="1.2"/>
			<param name="z" value="true"/>
		</import>
	</foos>

	<fragment location="root().foos()">

		<import url="foo.xml" name="foo_2">
			<param name="x" value="4"/>
			<param name="y" value="10"/>
			<param name="z" value="false"/>
		</import>

	</fragment>

</root>

You can see the introduction of the "jscript-like" syntax in the location attribute of the fragment. "root()" returns the root of the tree (the "root" object), and then "foos()" returns the composite member "foos" in the "root" object for placing the new object into.

To the running program, this XML file appears exactly the same as the last one.

Fragments are especially useful when trying to modularize the XML for a large tree. You might already have an XML which contains a heirarchy of objects, but then in another case you wish to include that heirarchy, but add a couple more objects to it (without modifying the original XML file).

Templates

You can include a special type of member in your object called xmlobj::xmlobj_templates whcih appears just like a vector, but objects can actually be subclassed off it.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<root>
	<templates>
		<foo name="foo_t">
			<x>1</x>
			<y>2.1</y>
			<z/>
		</foo>
	</templates>
	
	<foos>
		<foo_t name="foo_1"/>
		<foo_t name="foo_2"/>
		<foo_t name="foo_3">
			<x>5</x>
		</foo_t>
	</foos>

</root>

In this case, "foo_1" and "foo_2" will be new objects which have the same member values as "foo_t", and "foo_3" will be the same except for the "x" member.

Entire heirarchies of objects can be templated, and they will be correctly copied and cloned:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<window>

	<templates>
		<font name="default_font">
			<face>Verdana</face>
			<size>12</size>
		</font>
		<button name="button_t">
			<font>
				<default_font/>
			</font>
		</button>
	</templates>
	
	<panes>
		<button_t name="button_1"/>
		<button_t name="button_2"/>
		<button_t name="button_3">
			<font>
				<default_font>
					<face>Verdana</face>
					<size>24</size>
				</default_font>
			</font>
		</button_t>
	</panes>

</window>

This will become important when you try to implement really large XML files. It is also used in many of the projects that I have written to use this system.

Previous Next Usage Home Installation Docs


Generated: Wed Apr 5 23:00:19 EST 2006

Copyright (c) 2005; Paul Hamilton; pHamtec P/L.

Use, modification, and distribution is provided free of any limitations.