Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add encoding option for custom XML header #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions jsontoxml.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,19 @@ var process_to_xml = function(node_data,options){
};


var xml_header = function(standalone) {
var ret = ['<?xml version="1.0" encoding="utf-8"'];
var xml_header = function(standalone,encoding) {
var ret = ['<?xml version="1.0"'];

if(standalone) {
ret.push(' standalone="yes"');
}

if(encoding) {
ret.push(' encoding="' + encoding + '"');
} else {
ret.push(' encoding="utf-8"');
}

ret.push('?>');

return ret.join('');
Expand All @@ -147,7 +153,7 @@ module.exports = function(obj,options){

if(options.xmlHeader) {
// the user wants an xml header
xmlheader = xml_header(!!options.xmlHeader.standalone);
xmlheader = xml_header(!!options.xmlHeader.standalone,options.xmlHeader.encoding);
}

if(typeof options.docType != 'undefined') {
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,23 @@ test("creates open and close tag on empty body",function(t){
t.end()
});

var expected_with_header = '<?xml version="1.0" encoding="utf-8"?>' + expected;
test("creates correct object and add a generic header",function(t){
var result = jsonxml(input,{escape:true, xmlHeader:true});
t.equals(result,expected_with_header,' test should have generated correct xml');
t.end()
});

var expected_with_encoding_header = '<?xml version="1.0" encoding="ISO-10646-UCS-4"?>' + expected;
test("creates correct object and add a header with encoding",function(t){
var result = jsonxml(input,{escape:true, xmlHeader:{encoding:"ISO-10646-UCS-4"}});
t.equals(result,expected_with_encoding_header,' test should have generated correct xml');
t.end()
});

var expected_with_standalone_encoding_header = '<?xml version="1.0" standalone="yes" encoding="ISO-10646-UCS-4"?>' + expected;
test("creates correct object and add a header with standalone, encoding",function(t){
var result = jsonxml(input,{escape:true, xmlHeader:{standalone:true,encoding:"ISO-10646-UCS-4"}});
t.equals(result,expected_with_standalone_encoding_header,' test should have generated correct xml');
t.end()
});