On Sun, Nov 6, 2016 at 5:35 PM, Tong Sun wrote: > > On Saturday, November 5, 2016 at 3:51:55 PM UTC-4, Tong Sun wrote: >> >> >> On Saturday, November 5, 2016 at 3:42:27 PM UTC-4, Tong Sun wrote: >>> >>> >>> On Sat, Nov 5, 2016 at 12:26 PM, Sam Whited wrote: >>> >>>> On Fri, Nov 4, 2016 at 4:32 PM, Tong Sun wrote: >>>> > How to beautify a given XML string in GO?... >>> >>> I guess such thing doesn't exist, but let me ask away anyway -- the >>> following is exactly what I was looking for: >>> >>> from http://stackoverflow.com/questions/21117161: >>> >>> I like this solution, but am still in search of a Golang XML >>>> formatter/prettyprinter that doesn't rewrite the document (other than >>>> formatting whitespace). Marshalling or using the Encoder will change >>>> namespace declarations. For example an element like "<ns1:Element/>" will >>>> be translated to something like '<Element xmlns="ns1"></Element>' which >>>> seems harmless enough except when the intent is to not alter the xml other >>>> than formatting. – James McGill >>>> <http://stackoverflow.com/users/4979966/james-mcgill> Nov 12 '15 >>>> <http://stackoverflow.com/questions/21117161/go-how-would-you-pretty-print-prettify-html#comment55137594_27141132> >>> >>> >>> Using Sam's above code as an example, >>> >>> https://play.golang.org/p/JUqQY3WpW5 >>> >>> The above code format the following XML >>> >>> <soapenv:Envelope xmlns:soapenv="http://schemas. >>> xmlsoap.org/soap/envelope/" >>> xmlns:ns="http://example.com/ns"> >>> <soapenv:Header/> >>> <soapenv:Body> >>> <ns:request> >>> <ns:customer> >>> <ns:id>123</ns:id> >>> <ns:name type="NCHZ">John Brown</ns:name> >>> </ns:customer> >>> </ns:request> >>> </soapenv:Body> >>> </soapenv:Envelope> >>> >>> >>> into this: >>> >>> <Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/" >>> xmlns:_xmlns="xmlns" _xmlns:soapenv="http://schemas >>> .xmlsoap.org/soap/envelope/" _xmlns:ns="http://example.com/ns"> >>> <Header xmlns="http://schemas.xmlsoap.org/soap/envelope/"></Header> >>> <Body xmlns="http://schemas.xmlsoap.org/soap/envelope/"> >>> <request xmlns="http://example.com/ns"> >>> <customer xmlns="http://example.com/ns"> >>> <id xmlns="http://example.com/ns">123</id> >>> <name xmlns="http://example.com/ns" type="NCHZ">John Brown</name> >>> </customer> >>> </request> >>> </Body> >>> </Envelope> >>> >>> >>> I know they are the same in syntax, however they look totally different. >>> >>> Any way (e.g., to tweak encoding/xml) to make the beautified string look >>> closer to the original? >>> >> >> Well, I *got it working*. ..., and I'm *quite happy* with its look. I > believe it'd be much faster than any XML decode/encode route. *Starting > to convert the algorithm into Go code*... >
FTR, https://github.com/go-xmlfmt/xmlfmt/ HTH -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.