xml-js
xml-js copied to clipboard
How to keep empty lines when converting xml → js → xml?
Hello,
I'm editing a xml file and this xml has a few empty lines that keep the elements "organized". But, when I do the process of converting xml → js → xml
the final xml doesn't have any empty lines. Everything else is working super fine for me.
My xml:
<project>
<element1>element1</element1>
<element2>element2</element2>
<element3>element3</element3>
</project>
After editing it and converting to xml again, I have this:
<project>
<element1>element1</element1>
<element2>element2</element2>
<element3>element3</element3>
</project>
My question: is there a way to preserve the empty lines of my original xml?
@alencarrh
This is possible under a hidden option flag captureSpacesBetweenElements
.
Here is how to use it:
var convert = require('xml-js');
var test = `<project>
<element1>element1</element1>
<element2>element2</element2>
<element3>element3</element3>
</project>`
var resultJson = convert.xml2js(test, {captureSpacesBetweenElements: true});
var resultXml = convert.js2xml(resultJson);
console.log(resultXml);
Wow, that's an option to hide. Very happy I discovered this post <3
What is the reason the parameter is not currently documented? Is it experimental?
No, it is not experimental. This option was there in the early releases. I added it based on a user request. I forgot why I did not want to expose it. Maybe I wanted to discourage its usage as the output json has probably unwanted spaces. I will expose it soon in the readme documentation.