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?...
>
> ...If all you need is the bulit in indentation you can use
> an encoder and its indent method:
>
> https://godoc.org/encoding/xml#Encoder.Indent
>
> This is what MarshalIndent is doing under the hood. My example still
> applies, but you don't have to do it yourself. Instead you can just
> set the indentation on the encoder:
>
> https://play.golang.org/p/dVJjYvdHpS


I guess such thing doesn't exist, but let me ask away anyway -- the
following is exactly what I was looking for, couldn't express better than
his:

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?

Thanks

-- 
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.

Reply via email to