'use strict';

var xmlWrite = require('./xmlWrite');

var attributePrefix = '@';

function traverse(obj,parent) {
	
var result = [];
	
	for (var key in obj){
		// skip loop if the property is from prototype
		if (!obj.hasOwnProperty(key)) continue;

		var array = Array.isArray(obj);
		
		if (typeof obj[key] !== 'object'){
			if (key.indexOf(attributePrefix)==0) {
				xmlWrite.attribute(key.substring(1),obj[key]);
			}
			else {
				xmlWrite.startElement(key);
				xmlWrite.content(obj[key]);
				xmlWrite.endElement(key);
			}
		}
		else {
			if (!array) {
				xmlWrite.startElement(key);
			}
			else {
				if (key!=0) xmlWrite.startElement(parent);
			}
			traverse(obj[key],key);
			if (!array) {
				xmlWrite.endElement(key);
			}
			else {
				if (key!=(obj.length-1)) xmlWrite.endElement(parent);
			}
		}
	}
	return result;
}

module.exports = {
	getXml : function(obj,attrPrefix,standalone,indent,fragment) {
		if (attrPrefix) attributePrefix = attrPrefix;
		if (fragment) {
			xmlWrite.startFragment(indent);
		}
		else {
			xmlWrite.startDocument('UTF8',standalone,indent);
		}
		traverse(obj,'');
		return xmlWrite.endDocument();
	}
};
'use strict';

var xmlWrite = require('./xmlWrite');

var attributePrefix = '@';

function traverse(obj,parent) {

var result = [];

	var array = Array.isArray(obj);
	for (var key in obj) {
		// skip loop if the property is from prototype
		if (!obj.hasOwnProperty(key)) continue;

		var propArray = Array.isArray(obj[key]);
		var output = array ? parent : key;

		if (typeof obj[key] !== 'object'){
			if (key.indexOf(attributePrefix)==0) {
				xmlWrite.attribute(key.substring(1),obj[key]);
			}
			else {
				xmlWrite.startElement(output);
				xmlWrite.content(obj[key]);
				xmlWrite.endElement(output);
			}
		}
		else {
			if (!propArray) {
				xmlWrite.startElement(output);
			}
			traverse(obj[key],output);
			if (!propArray) {
				xmlWrite.endElement(output);
			}
		}
	}
	return result;
}

module.exports = {
	getXml : function(obj,attrPrefix,standalone,indent,fragment,indentStr) {
		if (attrPrefix) attributePrefix = attrPrefix;
		if (fragment) {
			xmlWrite.startFragment(indent,indentStr);
		}
		else {
			xmlWrite.startDocument('UTF8',standalone,indent,indentStr);
		}
		traverse(obj,'');
		return xmlWrite.endDocument();
	}
};
Fix array handling, pass indentStr to xmlWrite