MergeXML merges the XML sources (files, strings, objects) into single DOM XML object. The merging is performed recursively on the node level adding new elements and replacing existing ones. The nodes with the same path/name are replaced/added sequentially and the modification can be controlled by the options.
MergeXML could be useful in cases where it is necessary to gather XML data from multiple sources. For example, to combine configuration files of different subsystems depending on the application logic.
Main browsers (Chrome, Edge, IE, Firefox, Safari, Opera) and NodeJS (see below) are supported. The MergeXML is realized also in PHP (see php-merge-xml).
MergeXML class can be included:
- loading as a CommonJS module:
const MergeXML = require('./mergexml');
- as a global script:
The class instantiation:
var oMX = new MergeXML([opts]);
or loading and instantiating at once:
const oMX = new (require('./mergexml.js'))([opts]);
opts - the options object:
- join - common root name if any source has different root name (default is root, specifying false denies different names)
- updn - traverse the nodes by name sequence (true, default) or overall sequence (false)
- stay - the stay attribute value to deny overwriting of specific node (default is all, can be array of values or empty)
- path - require path to NodeJS modules (default is looking from node_modules, N/A for browsers)
source - XML string or DOM object
elem - FileList element of the XML file (browsers only)
The methods merge a sequent source and return the final object or false if failed (see error property below).
Clear existing result to restart.
opts - the options object as above (except path)
You can search in the result object:
expr - XPath query expression
You can get the XML result tree:
- 0 - object (default)
- 1 - text
- 2 - html
The result object can be accessed also via oMX.dom property. The properties available:
- dom - result XML DOM object (in older IE browsers this is an ActiveX Object, not a standard XML Document)
- nsp - namespaces list object (prefix:URI)
- count - number of sources merged
- error - error information
- error.code ('' is ok)
The sources must have the same default namespace (if have at all). Prefix '_' is reserved to handle the default namespace. IE doesn't allow replacement of the root node attributes.
Run from the appropriate directory:
npm install mergexml
Or manually download the js-merge-xml package from Github and unzip the files into installation directory.
Run the sample in your browser (HTML5 compatible):
- open example.html
- choose the xml files to be merged (test folder)
- click Merge button
- the merged xml is displayed...
To run the browsers' tests from the CLI:
- go to installation directory
- install framework:
npm install --dev
- run the tests:
- the results are displayed...
- to remove the test modules:
npm prune --prod
The browser window objects' (DOMParser, XPathEvaluator, XMLSerializer) functionality is implemented by the xpath, xmldom modules as node global objects. The sample requires also the formidable module.
Install the dependent modules:
npm install --prod
Start NodeJS with the sample script:
Run the sample in your browser:
The following files are included:
- mergexml.js - MergeXML class supporting the browser and NodeJS environments;
- example.html - multi-selects the xml files and displays result (browser);
- example.js - passes the xml data and returns result (browser);
- examplen.htm - client-side template to multi-select the xml files and display result (NodeJS);
- examplen.js - server-side module to receive requests and response results (NodeJS);
- package.json, bower.json - package details;
- test - a folder with the (browser) testing files.
June/July 2015 (Martijn van de Rijdt)
- the wrapper is added for a compatibility with the AMD/CommonJS-like environments
- automated browser tests
October 2016 (Martijn van de Rijdt)
- cloning the namespaced attributes correctly
- mixing sources of undeclared encoding
August 2019 (Vallo Reima)
- NodeJS environment support
- examplen.js, examplen.htm
- NodeJS usage sample