0


I'm using angularjs to develop my application. I use a JSON variable which I want to convert into xml. I do it with the following code :

My JSON variable :

variableJSON = {
    "content": {
      "section": []
    },
    "field": {
      "string": [],
      "integer": [],
      "decimal": [],
      "list": [],
      "iterator": []
    },
  };

I fill it when my application run.


And this is my code to convert it :

x2js = new X2JS({
    escapeMode: false,
  });

var xmlVar = $scope.x2js.json2xml_str(variableJSON);

This is an adaptation. My real code isn't like this so it's possible it's not working with the code here.

Finally I have this :

<content><section id='S0'><label lang='en'>Section 1</label><single name='Q0'><label lang='en'>Question 1</label></single></section></content><field></field>

As you can see, there is only one line. I would like to skip line for each different tags to have a proper view of what I generated. But I don't want to do it after the generation. When the XML is generated I must keep it intact. I couldn't find a solution on x2js website or on any forum.

Do you have a solution to skip line during the XML generation ?

Thank you in advance.

Mtoypc
  • 464
  • 1
  • 6
  • 24

1 Answers1

1

I am assuming the mentioned xml2json is x2js. If this is true, then there is no way to indent the resulting XML.

The best options for you are:

  1. Use another JSON to XML library that has the option for indentation.
  2. Use some pretty printing library that will format your one-line XML into indented XML. (see Pretty printing XML with javascript for example)
Community
  • 1
  • 1
Sulthan
  • 128,090
  • 22
  • 218
  • 270