[xsd-users] Need help: how to merge multiple xml files

Boris Kolpackov boris at codesynthesis.com
Mon Oct 11 12:05:46 EDT 2010


Hi JW,

JW <jianwei.z at gmail.com> writes:

> 1. full_elem.xml will have all elements ever required.  Say, <configs> is
> the root.
> 
> <configs>
>   <config id="1" name="config1">
>     <threshold>10</threshold>
>     <duration>60</duration>
>   </config>
>   <config id="2" name="config2">
>     <threshold>20</threshold>
>     <duration>120</duration>
>   </config>
> </configs>
> 
> 2. override_elem.xml will have part of elements that their elements will be
> overwritten.
> 
> <configs>
>   <config id="1">
>     <threshold>100</threshold>
>   </config>
>   <config id="2">
>     <duration>180</duration>
>   </config>
> </configs>
> 
> After the merging, the final full_elem.xml would look like the following:
> 
> <configs>
>   <config id="1" name="config1">
>     <threshold>100</threshold>
>     <duration>60</duration>
>   </config>
>   <config id="2" name="config2">
>     <threshold>20</threshold>
>     <duration>180</duration>
>   </config>
> </configs>

Ok, this is much more useful. There are two ways to do this that I can
think of. The first is using the object model. In this case, you will
need to iterate over the config elements in the first document and
for each config object you will need to find the corresponding config
in the second document. Once you have the two objects, you can set
the values in the object from the first document using the values
from the object from the second document. With this approach you
will need to know which members can be present in the config object:

config_t& dst = ...
config_t& src = ...

if (src.threshold ().present ()) // assuming threshold is optional
{
  dst.threshol (src.threshold ());
}

dst.duration (src.duration ()); // assuming threshold is required

It may be easier (and faster) to first create a map of ids to config
object for the second document.

The other approach is more complicated but it will work without any
modifications if you add new elements to the config type. The idea
is to use the DOM representation to merge the two documents before
parsing the result into an object model. The implementation will
be quite a bit more complex so I don't recommend it unless you are
proficient with Xerces-C++ DOM API and plan to change your schema
often.

Boris



More information about the xsd-users mailing list