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

json/ js to xml conversion looses empty arrays information #398

Open
manoharreddyporeddy opened this issue Aug 17, 2017 · 4 comments
Open

Comments

@manoharreddyporeddy
Copy link

json/ js to xml conversion looses empty arrays information

An empty string is converted to an empty tag.
An empty array is Not converted to an empty tag.

However, we needed for our functionality so that clients don't break, for example, when card numbers are not given, an empty tag exists

example:

let jsonObj = {
	key1: 'val2',
	arrayKey1: [456],
	emptyKey1: '',
	emptyArrayKey1: []
};

var builder = new xml2js.Builder();
var xml = builder.buildObject(jsonObj);
console.log(xml);

actual output (NOTE: emptyArrayKey1 tag is missing):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
  <key1>val2</key1>
  <arrayKey1>456</arrayKey1>
  <emptyKey1/>
</root>

expected output (NOTE: emptyArrayKey1 tag is now back):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
  <key1>val2</key1>
  <arrayKey1>456</arrayKey1>
  <emptyKey1/>
  <emptyArrayKey1/>
</root>
@manoharreddyporeddy
Copy link
Author

I have fixed it using below code, using JavaScript, writing fix details below, because I don't know Coffee Script to create pull-request, if fix is good in general too, then please help others via a pull request:


Fix details

in the file:

.\xml2js\lib\builder.js
OR
https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/lib/builder.js

below code:

	} else if (Array.isArray(child)) {
		for (index in child) {

should be changed to:

	} else if (Array.isArray(child)) {

		// NOTE: FIX - begin
		if (child.length === 0) {
			element = render(element.ele(key), '').up();
		}
		// NOTE: FIX - end

		for (index in child) {

@Leonidas-from-XIV
Copy link
Owner

Leonidas-from-XIV commented Aug 17, 2017 via email

@Leonidas-from-XIV
Copy link
Owner

Oh, I didn't see your message. Well, I can only apply it when I have some spare time.

@jcsahnwaldt
Copy link
Contributor

I'm not sure this needs a fix in xml2js. If you want an empty element in your XML, just add an empty string to your array:

let jsonObj = {
	key1: 'val2',
	arrayKey1: [456],
	emptyKey1: '',
	emptyArrayKey1: ['']
};

var builder = new xml2js.Builder();
var xml = builder.buildObject(jsonObj);
console.log(xml);

Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
  <key1>val2</key1>
  <arrayKey1>456</arrayKey1>
  <emptyKey1/>
  <emptyArrayKey1/>
</root>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants