oozcitak/xmlbuilder-js — pull request #20 by piroor (2012-07-26 09:13:48+00:00)
Title: Implement importXMLBuilder()

The java-xmlbuilder has a method "importXMLBuilder()" to import built contents from another builder. It is necessary for building one large or complex XML document from sub fragments. These commits introduce XMLFragment.prototype.importXMLBuilder() (and XMLFragment.prototype.clone(), it is internally used.)

Example of actual usecase: [building modularized XML responses](https://github.com/groonga/gcs/blob/master/lib/api/2011-02-01/configuration.js).

function createIndexFieldOptionStatus(options) {
switch (options.fieldType) {
case 'text':
var textOptions = xmlbuilder.create();
textOptions.begin('TextOptions', { version: '1.0' })
.element('DefaultValue').up()
.element('FacetEnabled').text(options.facetEnabled || 'false').up()
.element('ResultEnabled').text(options.resultEnabled || 'false');
return textOptions;
...
}
}

function createIndexFieldStatus(options) {
var indexFieldStatus = xmlbuilder.create();
indexFieldStatus.begin('IndexField', { version: '1.0' })
.element('Options')
.element('IndexFieldName').text(options.fieldName).up()
.element('IndexFieldType').text(options.fieldType).up()
.importXMLBuilder(createIndexFieldOptionStatus(options))
.up()
...;
return indexFieldStatus;
}

function createDefineIndexFieldResponse(options) {
var doc = xmlbuilder.create();
doc.begin('DefineIndexFieldResponse', { version: '1.0' })
.attribute('xmlns', XMLNS)
.element('DefineIndexFieldResult')
.importXMLBuilder(createIndexFieldStatus(options))
.up()
.element('ResponseMetadata')
.element('RequestId').text(options.requestId || '').up()
.up();
return doc.toString();
}