xml-js icon indicating copy to clipboard operation
xml-js copied to clipboard

How to keep empty lines when converting xml → js → xml?

Open alencarrh opened this issue 5 years ago • 3 comments

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 avatar Feb 26 '19 14:02 alencarrh

@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);

nashwaan avatar Mar 04 '19 11:03 nashwaan

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?

simlu avatar Mar 14 '19 05:03 simlu

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.

nashwaan avatar Mar 14 '19 05:03 nashwaan