Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 2 of 3
You've seen a few examples of applying XmlMerge to common Java enterprise development scenarios. l'll spend the remainder of this article explaining how the tool works. By default, you can use XPath declarations to specify how XmlMerge handles your XML sources. A sample configuration is shown in Listing 6.
Listing 6. xmlmerge.properties
action.default=COMPLETE # By default, only add elements not
# already existing in first file
XPath.a=/root/a # define a XPath named "a" and matching
# all <a> elements under <root>
action.a=MERGE # configure to merge children of <a>
Listing 7 shows two more files that need to be merged, this time as specified by the above XPath declarations.
Listing 7. Two XML files waiting to be merged
| Original | Patch |
|
|
Listing 8 shows the files merged as specified by the XPath declarations.
Listing 8. Two files merged as specified
<root>
<a>
<b/> # merged the content of the element <a>
</a>
<c/> # by default, do not modify existing elements
</root>
Using XPath declarations within the XmlMerge utility lets you specify how each element in your XML files will be handled during a merge. In the next section I'll explain the actions you may have noticed in Listing 5, as well as the use of matching functions in XmlMerge.
XmlMerge provides many built-in actions, some of them extending its functions well beyond simple merging. Consider the following actions and the various ways you could use XPath declarations to apply them to elements in your XML documents.
Table 1. Built-in actions for XmlMerge
| Action | Description | Result |
MERGE |
Traverses in parallel the original and patch elements, determines matching pairs between documents in the order of traversal,
and merges children recursively. MERGE is the default action and is sufficient for most common uses where the original and patch documents present elements in the
same order.
|
|
REPLACE |
Replaces original elements with patch elements. Can also be used to add new (patch) elements to a file. |
|
OVERRIDE |
Replaces an original element with a patch element. |
|
COMPLETE |
Selectively adds in patch elements that did not exist in the original, using patch elements to complete the original ones. |
|
DELETE |
Copies the original element only if it does not exist in the patch. If it exists in the patch, then nothing is added to the result (the presence of patch elements actually deletes the matching elements from the original). |
|
PRESERVE |
Invariantly copies the original element regardless of the existence of the patch element (it drops the patch element). |
|
It is also possible to tell XmlMerge that elements from the original and the patch correspond to criteria other than the element name. For this you would use matching functions, or matchers.
Table 2. Built-in matchers for XmlMerge
| Matcher | Description |
TAG |
This default matcher says the original and patch elements match if the tag name is the same. |
ID |
The original and patch elements match if the tag names and the id attribute values are the same. |
Archived Discussions (Read only)