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

marshalling to string with pretty print #235

Open
br8kpoint opened this issue Nov 15, 2019 · 3 comments
Open

marshalling to string with pretty print #235

br8kpoint opened this issue Nov 15, 2019 · 3 comments

Comments

@br8kpoint
Copy link

As far as I can see, there is no way to marshalString to provide formatted output that would be suitable for saving to a file.

Am I missing something?

@highsource
Copy link
Owner

No, there is no such way at the moment. Jsonix uses XMLSerializer for serialization and it does not support pretty printing.

@Knochenmark
Copy link

I recently stumbled across this too and was hoping for an easy config option to output a pretty printed string.

@bevanweiss
Copy link

I've got something of a 'solution'.
It's not quite the most pleasant, but it should be quite flexible for any additional changes required (albeit needing those changes to be implemented within an xslt environment)

    var xsltDoc = new DOMParser().parseFromString(
      [
        // describes how we want to modify the XML - indent everything
        "<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>",
        "  <xsl:output omit-xml-declaration='no' indent='yes'/>",
        "  <xsl:template match='node()|@*'>",
        "    <xsl:copy>",
        "      <xsl:apply-templates select='node()|@*'/>",
        "    </xsl:copy>",
        "  </xsl:template>",
        "</xsl:stylesheet>",
      ].join("\n"),
      "application/xml"
    );

    var xsltProcessor = new XSLTProcessor();
    xsltProcessor.importStylesheet(xsltDoc);
    var pretty_doc = xsltProcessor.transformToDocument(doc);

One problem is that it will strip out CDATA sections. If you know ahead of time which you want to retain then you can add the following attribute to the xsl:output element
cdata-section-elements='elementType1 elementType2 elementType3 ...'

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

4 participants