JS Merge XML: Merge XML documents from strings or DOM objects

Recommend this page to a friend!

  Author Author  
Name: Vallo Reima <contact>
Classes: 6 packages by
Country: Estonia Estonia
Age: ???
All time rank: 351 in Estonia Estonia
Week rank: 1 Up
Innovation award
Innovation award
Nominee: 4x

Winner: 1x

  Detailed description   Download Download .zip .tar.gz   Install with Composer Install with Composer  
This object can merge XML documents from strings, DOM objects or input files.

It can take a XML document from a string or a DOM object tree, or a file taken from a form input and merges with another XML document.

The resulting document can be search using a XPath query string or returned as a string or a DOM object.

This object provides options to control whether the nodes with the same path or name are replaced or added sequentially.

JS MergeXML [![npm version](https://badge.fury.io/js/mergexml.svg)](https://badge.fury.io/js/mergexml)

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 join the configuration files of different subsystems depending on the system operation. 

Supports main browsers (Chrome, Edge, IE, Firefox, Safari, Opera) and NodeJS (see below).

The usage

**`<script src="mergexml.js"></script>`**

**`const MergeXML = require('mergexml');`**

MergeXML can be included as a global script or with `npm install mergexml --save` and loaded as shown above.

**var oMX = new MergeXML([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)


> elem - FileList element of the XML file


> source - XML string or DOM object

The methods merge a sequent source and return the final object or *false* if failed (see *error* property below).

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 - **Note that in older IE browsers this is an ActiveX Object and not a standard XML Document!**
- **nsp** - namespaces object (prefix:URI)
- **count** - number of sources merged
- **error** - error information
 - error.code ('' is ok)
 - error.text

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.

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 modules by starting **NPM** from the directory where you have downloaded the package files:

>npm install xpath xmldom formidable

Start the NodeJS service with the sample script:

>node examplen.js

Run the sample in your browser:


The package

The examples require **HTML5**. The following files are included:

1. *mergexml.js* - MergeXML class supporting the browser and NodeJS environments;
2. *example.html* - multi-selects the xml files and displays result (browser);
3. *example.js* - passes the xml data and returns result (browser);
4. *examplen.htm* - client-side template to multi-select the xml files and display result (NodeJS);
5. *examplen.js* - server-side module to receive requests and response results (NodeJS);
6. *test1.xml, test2.xml* - test data for the examples;
7. *package.json, bower.json* - package details;

The MergeXML is realized also in PHP (see [github.com]).


June 2015

- *mergexml.js*
  - the wrapper is added for a compatibility with the AMD/CommonJS-like environments
October 2016 (Martijn van de Rijdt)

- *mergexml.js*
  - cloning the namespaced attributes correctly
  - mixing sources of undeclared encoding

July 2019

- *mergexml.js*
  - NodeJS environment support
- *examplen.js, examplen.htm*
  - NodeJS usage sample
  [github.com]: http://www.github.com/hareko/php-merge-xml

  Classes of Vallo Reima  >  JS Merge XML  >  Download Download .zip .tar.gz  >  Support forum Support forum  >  Blog Blog  >  RSS 1.0 feed RSS 2.0 feed Latest changes  
Name: JS Merge XML
Base name: merge-xml
Description: Merge XML documents from strings or DOM objects
Version: 1.1.6
JavaScript version: 1.4
License: BSD License
All time users: 112 users
All time rank: 392
Week users: 8 users
Week rank: 1 Up
  Groups   Rate classes User ratings   Applications   Files Files  

Group folder image XML XML parsing and generation View top rated classes

  Innovation Award  
JavaScript Programming Innovation award nominee
May 2014
Number 6
In JavaScript it is easy to create and parse XML documents using the DOM support built-in all browsers.

This object extends that support by making it easier to merge XML nodes from multiple documents.

Manuel Lemos

  User ratings  
Not enough user ratings

  Applications that use this package  
No pages of applications that use this class were specified.

Add link image If you know an application of this package, send a message to the author to add a link here.

  Files folder image Files  
File Role Description
Accessible without login Plain text file bower.json Data Auxiliary data
Accessible without login Plain text file example.html Example Sample output
Accessible without login Plain text file example.js Example Example script
Accessible without login HTML file examplen.htm Doc. Documentation
Accessible without login Plain text file examplen.js Example Example script
Accessible without login Plain text file LICENSE Doc. Documentation
Plain text file mergexml.js Class Class source
Accessible without login Plain text file package-lock.json Data Auxiliary data
Accessible without login Plain text file package.json Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file test1.xml Data Auxiliary data
Accessible without login Plain text file test2.xml Data Auxiliary data

Install with Composer Install with Composer - Download Download all files: merge-xml.tar.gz merge-xml.zip
NOTICE: if you are using a download manager program like 'GetRight', please Login before trying to download this archive.