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

New Line After each tag #40

Closed
PhuocHoangMinhNguyen opened this issue Aug 15, 2020 · 3 comments
Closed

New Line After each tag #40

PhuocHoangMinhNguyen opened this issue Aug 15, 2020 · 3 comments
Assignees
Labels

Comments

@PhuocHoangMinhNguyen
Copy link

PhuocHoangMinhNguyen commented Aug 15, 2020

How can I make new line after each tag? For example:
<a>A Value</a>
<b>B Value</b>
Instead of: <a>A Value</a><b>B Value</b>

I'm using "jstoxml": "^1.6.8"

@davidcalhoun
Copy link
Owner

Hi, good question. I think there may be a small bug in the library right now, this seems like it should output with line breaks when you use the indent option, but it's on one line as you describe:

toXML([
  {a: "A Value"},
  {b: "B Value"}
], {indent: '  '});

Results in

<a>A Value</a><b>B Value</b>

But seems like it should result in this? (I'll take a look and see if I can tweak this)

<a>A Value</a>
<b>B Value</b>

You can get line breaks when you nest the value with indenting, e.g.

toXML({
  foo: [
    {a: "A Value"},
    {b: "B Value"}
  ]
}, {indent: '  '});

Which results in this:

<foo>
  <a>A Value</a>
  <b>B Value</b>
</foo>

Note that you can also force line breaks between tags with this:

toXML([
  {a: "A Value"},
  '\n',
  {b: "B Value"}
]);

Which results in this:

<a>A Value</a>
<b>B Value</b>

@PhuocHoangMinhNguyen
Copy link
Author

Thanks for replying. I did find a workaround for it when showing the XML file content on the screen, which were entering this line of code into App.css.
div {
white-space: pre-wrap;
}
However, I'm not sure if that gonna work when I want to enter the information into an actual XML file and download it (which I don't think it will work). So I am looking forward to receiving more information from you. I will try "force line breaks between tags" for now.

@davidcalhoun
Copy link
Owner

@PhuocHoangMinhNguyen Yep, if you see the new lines with that CSS, then the new line characters should be in the XML file output when you download it as well.

Just a heads up that I added a fix for the original issue.

This code:

toXML([
  {a: "A Value"},
  {b: "B Value"}
], {indent: '  '});

or this code:

toXML({
  a: "A Value",
  b: "B Value"
}, {indent: '  '});

Will now result in this:

<a>A Value</a>
<b>B Value</b>

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

No branches or pull requests

2 participants