Tuesday, September 19, 2006

Pragmatic merging of XML

Sometimes it is useful to be able to merge different XML document into one. For example when a tool accepts only one XML configuration file, but different subprojects that use the tool together would each be able to add some information to the XML configuration file. A concrete example would be the web.xml file of a J2EE servlet container: there can only be one such file per WAR-file. What if you have 2 subprojects that you would like to deploy into one WAR-file?
You could merge the files by hand or you could set up an merging functionality to do it automatically for you. With the little framework I present here, you can easily set up your own XML file merging. The framework is available under the EL4J project (http://EL4J.sf.net) as part of the xml merger module. It was contributed by Laurent Bovet (from outside of the EL4J core team), thanks.

Let's look at a simple example:



The following simple code implements this:

public String merge(String original, String patch) {
Configurer c = new PropertyXPathConfigurer("xpath.1=/root/d \n matcher.1=ID");

return new ConfigurableXmlMerge(c).merge(new String[]{original, patch} );

}


You may ask: what does it mean to merge XML documents? I think it is difficult to find a general answer to this question:
  • Sometimes the second XML file should be able override tags of the first XML file
  • What are the branches that should be merged? Just the same XML tags? Or does some attribute need to be equal?
  • Sometimes some branches of the XML document may need to be left out
Because of this, the XML merger is set up as a framework. You plug in your own definitions of how matching and mappings are done. There are basic merging semantics implemented in the framework. If you need other rules, you can extend the parts that you are interested in.

Please consult the detailed documentation or the samples of the xml merger in case you would like to know more.

No comments: