FAQ
i'm using encoding/xml and find you can get basic pretty printing using
"MarshalIndent",

but since i have a large dataset, i need to use "Encode" directly, to be
able to write to a stream(file) and avoid the complete duplicate data copy
in memory being made from "Encode" by "Marshal*" .

which is fine to get basic output, but since there's no "EncodeIndent", it
isn't pretty.

when i check the code, this is controlled by the unexported;
Encoder.printer.indent and Encoder.printer.prefix parameters, so i seem to
be stuck.

or am i missing something?

--

Search Discussions

  • Philipp Schumann at Nov 19, 2012 at 12:39 pm
    If you have many separate individual Encode calls, couldn't you just flush
    out tabs yourself manually in between them?



    On Monday, November 19, 2012 9:24:28 AM UTC+8, simon...@googlemail.com
    wrote:
    i'm using encoding/xml and find you can get basic pretty printing using
    "MarshalIndent",

    but since i have a large dataset, i need to use "Encode" directly, to be
    able to write to a stream(file) and avoid the complete duplicate data copy
    in memory being made from "Encode" by "Marshal*" .

    which is fine to get basic output, but since there's no "EncodeIndent", it
    isn't pretty.

    when i check the code, this is controlled by the unexported;
    Encoder.printer.indent and Encoder.printer.prefix parameters, so i seem to
    be stuck.

    or am i missing something?
    --
  • Simon Place at Nov 19, 2012 at 4:08 pm

    On Monday, 19 November 2012 12:39:34 UTC, Philipp Schumann wrote:
    If you have many separate individual Encode calls, couldn't you just flush
    out tabs yourself manually in between them?
    dont think so, Encode uses "marshalValue" which is recursive, so no way to
    get between levels. (i guess you're thinking of a single level 'flat'
    structure when you are always at a leaf node.)

    im now thinking that if i really need this i'll have to fork the xml
    library and add exposed setter functions, for these options, seems quite
    painless. (apart from maintaining a fork.)

    --
  • Nkothap at Jan 26, 2013 at 1:54 am
    I have the same issue here. In the example below I get an xml file but an
    unformatted/unindented one. I tried using MarshalIndent() here, but it
    returns []byte type and I need to copy it to a struct pointer.

    type XMLMonitoringData struct {
    XMLName xml.Name `xml:"monitoring_data"`
    Controllers *XMLControllers `xml:"Controllers"`
    blah blah
    }

    func MarshalMonitoringData(writer io.Writer, mon_data XMLMonitoringData)
    error {

    if _, err := writer.Write([]byte(xml.Header)); err != nil {
    return err
    }

    xmlMonitoringData := XMLFill_MD(mon_data)
    encoder := xml.NewEncoder(writer)
    return encoder.Encode(xmlMonitoringData)
    }

    func XMLFill_MD(mon_data XMLMonitoringData) *XMLMonitoringData {

    xmlMonitoringData := &XMLMonitoringData{
    Drives: mon_data.Drives,
    }

    xmlMonitoringData.Controllers = XMLFill_Controllers()

    return xmlMonitoringData
    }

    Any thoughts?

    Thank you!


    On Monday, November 19, 2012 9:08:30 AM UTC-7, simon...@googlemail.com
    wrote:

    On Monday, 19 November 2012 12:39:34 UTC, Philipp Schumann wrote:

    If you have many separate individual Encode calls, couldn't you just
    flush out tabs yourself manually in between them?
    dont think so, Encode uses "marshalValue" which is recursive, so no way to
    get between levels. (i guess you're thinking of a single level 'flat'
    structure when you are always at a leaf node.)

    im now thinking that if i really need this i'll have to fork the xml
    library and add exposed setter functions, for these options, seems quite
    painless. (apart from maintaining a fork.)
    --
  • Simon Place at Jan 27, 2013 at 11:04 pm
    not sure what you're doing, but dont think its the same 'problem', for me
    its that to get pretty xml you have to go thought a []byte, ie have the
    whole text in memory at once, when internally, (and also when not pretty),
    the code is using a memory efficient stream, and also that, although
    actually all there, strangely, the code is so structured as to prevent the
    underlying streaming codes reuse.

    --
  • Simon Place at Jan 27, 2013 at 11:05 pm
    in summary:

    for xml files being printed pretty, you seem to be in the
    horrible situation of needing to be sure you have memory available for a
    potentially unknown size of xml.

    i was hoping someone would show me a clever way round the codes structure
    to get this functionally or agree with me that this needs improving.

    --
  • Russ Cox at Jan 30, 2013 at 3:23 pm
    https://codereview.appspot.com/7221075

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Simon place at Feb 13, 2013 at 11:24 pm
    was wondering when this update/feature would be in the standard lib, just
    noticed it is in the new app engine 1.7.5 and also the playgound!

    http://play.golang.org/p/WiDVaW3Nrm

    nice.


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Simon place at Feb 14, 2013 at 1:12 am
    whoops!, i was reading it wrong, it hasn't changed in the release yet.
    On Wednesday, 13 February 2013 23:22:45 UTC, simon place wrote:

    was wondering when this update/feature would be in the standard lib, just
    noticed it is in the new app engine 1.7.5 and also the playgound!

    http://play.golang.org/p/WiDVaW3Nrm

    nice.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 19, '12 at 1:24a
activeFeb 14, '13 at 1:12a
posts9
users4
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase