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, 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? 
>>
>
> FTR, trying to look into that direction myself, 
>
> The dark voodoo regexp as described here works for many cases.
> http://www.perlmonks.org/?node_id=261292
>
> $ echo '<root><this><is>a</is><test /><message><org><cn>Some 
> org-or-other</cn><ph>Wouldnt you like to 
> know</ph></org><contact><fn>Pat</fn><ln>Califia</ln></contact></message></this></root>'
>  
> | perl -pe 's/(?<=>)\s+(?=<)//g; 
> s(<(/?)([^/>]+)(/?)>\s*(?=(</?))?)($indent+=$3?0:$1?-1:1;"<$1$2$3>".($1&&($4 
> eq"</")?"\n".("  "x$indent):$4?"\n".("  "x$indent):""))ge' 
> <root>
>   <this>
>     <is>a</is>
>     <test />
>     <message>
>       <org>
>         <cn>Some org-or-other</cn>
>         <ph>Wouldnt you like to know</ph>
>         </org>
>       <contact>
>         <fn>Pat</fn>
>         <ln>Califia</ln>
>         </contact>
>       </message>
>     </this>
>   </root>
>
>
> $ echo '<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>' 
> | perl -pe 's/(?<=>)\s+(?=<)//g; 
> s(<(/?)([^/>]+)(/?)>\s*(?=(</?))?)($indent+=$3?0:$1?-1:1;"<$1$2$3>".($1&&($4 
> eq"</")?"\n".("  "x$indent):$4?"\n".("  "x$indent):""))ge' 
> <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>
>
> But I did found its own limits when trying more samples... 
>


Well, despite all the odds and the common believes, I *got it working*. 
After all, XML is much more structured than HTML. Here is what it was 
previously:

$ echo '<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>' | perl 
-pe 's/(?<=>)\s+(?=<)//g; 
s(<(/?)([^/>]+)(/?)>\s*(?=(</?))?)($indent+=$3?0:$1?-1:1;"<$1$2$3>".($1&&($4 
eq"</")?"\n".("  "x$indent):$4?"\n".("  "x$indent):""))ge'
<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>



After simplified the algorithm, it became the following, 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*... 

$ echo '<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>' | perl -pe 
's/(?<=>)\s+(?=<)//g; 
s(<(/?)([^>]+)(/?)>)($indent+=$3?0:$1?-1:1;"<$1$2$3>"."\n".(" 
 "x$indent))ge'

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


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