Sublime Forum

XML encode/decode?

#1

Hello,

Does have any way to encode/decode a XML string? I search in Sublime Text 2 itself and plugin, but have not find it.

For example:

<?xml version="1.0" encoding="UTF-8"?> <IntMsg><CommandID>CMD06201_VDESIMYO</CommandID><ExtTransID>0adc30787e6f49c8817511df2dae89dd</ExtTransID><ProcessTime>20120403183730</ProcessTime><DealerCode></DealerCode><OutOperID>BLAU ISP|esbblau</OutOperID><OutPwd></OutPwd><LoginType></LoginType><PUBLoginID></PUBLoginID><ContactID></ContactID><BrandID>100004900</BrandID><SubBrandID>220</SubBrandID><BrandPwd></BrandPwd><TestFlag>0</TestFlag><Response><RspCode>831331</RspCode><RspDesc>Rateplan is not exist.</RspDesc></Response><SvcCont></SvcCont></IntMsg>

After XML decode, it is:

<?xml version="1.0" encoding="UTF-8"?> <IntMsg><CommandID>CMD06201_VDESIMYO</CommandID><ExtTransID>0adc30787e6f49c8817511df2dae89dd</ExtTransID><ProcessTime>20120403183730</ProcessTime><DealerCode></DealerCode><OutOperID>BLAU ISP|esbblau</OutOperID><OutPwd></OutPwd><LoginType></LoginType><PUBLoginID></PUBLoginID><ContactID></ContactID><BrandID>100004900</BrandID><SubBrandID>220</SubBrandID><BrandPwd></BrandPwd><TestFlag>0</TestFlag><Response><RspCode>831331</RspCode><RspDesc>Rateplan is not exist.</RspDesc></Response><SvcCont></SvcCont></IntMsg>

Thank you very much!

0 Likes

#2

Encode is available by Command Palette:

{ "caption": "HTML: Encode Special Characters", "command": "encode_html_entities" }

Don’t think decode exists but you can do it yourself by writing a plugin based on \Sublime Text 2\Packages\HTML\encode_html_entities.py source file.
Use name2codepoint instead of codepoint2name.

Ask if you need more help.

0 Likes

#3

[quote=“bizoo”]Encode is available by Command Palette:

{ "caption": "HTML: Encode Special Characters", "command": "encode_html_entities" }

Don’t think decode exists but you can do it yourself by writing a plugin based on \Sublime Text 2\Packages\HTML\encode_html_entities.py source file.
Use name2codepoint instead of codepoint2name.

Ask if you need more help.[/quote]

Thank you for your help to quickly.

I am a beginner of Sublime Text 2, it’s too hard for me to make a plugin (but I am try to do it). I try to do it with another way: marco. But it aways give me a empty marco after I record/stop. :unamused:

0 Likes

#4

[quote=“tibell”]

[quote=“bizoo”]Encode is available by Command Palette:

{ "caption": "HTML: Encode Special Characters", "command": "encode_html_entities" }

Don’t think decode exists but you can do it yourself by writing a plugin based on \Sublime Text 2\Packages\HTML\encode_html_entities.py[/quote]

source file.
Use name2codepoint instead of codepoint2name.

Ask if you need more help.

Thank you for your help to quickly.

I am a beginner of Sublime Text 2, it’s too hard for me to make a plugin (but I am try to do it). I try to do it with another way: marco. But it aways give me a empty marco after I record/stop. :unamused:[/quote]

The default “encode_html_entities” wasn’t as aggressive as the the encoding string you showed. So I threw together a quick plugin that should convert back and forth between the methods you showed. The encode function was taken from the HTML package and just modified to be more aggressive so I could toggle back and forth between the two examples you showed. The decode was just pulled from an example on the web.

It has only been lightly tested, but you can probably adapt this to whatever you wish.

[code]import sublime_plugin
import re
from htmlentitydefs import name2codepoint as n2pc
from htmlentitydefs import codepoint2name as cp2n

class DecodeHtmlSpecialChars(sublime_plugin.TextCommand):
def run(self, edit, **args):
view = self.view

    def decode(s):
        return re.sub('&(%s);' % '|'.join(n2pc), lambda m: unichr(n2pc[m.group(1)]), s)

    for sel in view.sel():
        view.replace(edit, sel, decode(view.substr(sel)))

class EncodeHtmlSpecialChars(sublime_plugin.TextCommand):
def run(self, edit, **args):
view = self.view

    def encode(s):
        buf = ]
        for ch in s:
            ch_ord = ord(ch)

            if (ch_ord in cp2n):
                ch = '&%s;' % cp2n[ch_ord]

            buf.append(ch)

        return ''.join(buf)

    for sel in view.sel():
        view.replace(edit, sel, encode(view.substr(sel)))[/code]
0 Likes

#5

Use hasher plugin. CTRL+SHIFT+P > Entity Encode or Decode

0 Likes

#6

Even better, it is so hard to keep track of all of these plugins; there are so many now. Thanks for the heads up.

0 Likes

#7

It’s work! I installed plugin ‘Hasher’, and selected all content, use Ctrl+Shift+P - Hasher: Entity Decode.

:smiley:

0 Likes