diff --git a/Default.sublime-commands b/Default.sublime-commands
index 92ca6cb..c43f5b0 100644
--- a/Default.sublime-commands
+++ b/Default.sublime-commands
@@ -7,6 +7,10 @@
"caption": "Pretty JSON: Minify (compress) JSON",
"command": "un_pretty_json"
},
+ {
+ "caption": "Pretty JSON: JSON 2 XML",
+ "command": "json_to_xml"
+ },
{
"caption": "Pretty JSON: JSON query with ./jq",
"command": "jq_pretty_json"
diff --git a/PrettyJson.py b/PrettyJson.py
index 10aed5b..3c12baa 100644
--- a/PrettyJson.py
+++ b/PrettyJson.py
@@ -4,6 +4,8 @@ import decimal
import sys
import os
import re
+import xml.etree.ElementTree as ET
+from xml.dom import minidom
try:
# python 3 / Sublime Text 3
@@ -168,6 +170,71 @@ class JqPrettyJson(sublime_plugin.WindowCommand):
sublime.status_message(str(exc))
+class JsonToXml(PrettyJsonCommand):
+ """
+ converts Json to XML
+ """
+ def run(self, edit):
+ """ overwriting base class run function to remove intent """
+ self.view.erase_regions('json_errors')
+ for region in self.view.sel():
+
+ selected_entire_file = False
+
+ # If no selection, use the entire file as the selection
+ if region.empty() and s.get("use_entire_file_if_no_selection", True):
+ selection = sublime.Region(0, self.view.size())
+ selected_entire_file = True
+ else:
+ selection = region
+
+ try:
+ h = json.loads(self.view.substr(selection))
+ root = ET.Element("root")
+ root = self.traverse(root, h)
+
+ xml_string = ""
+ rtn = ET.tostring(root, "utf-8", "xml")
+
+ if type(rtn) is bytes:
+ rtn = rtn.decode("utf-8")
+
+ xml_string += rtn
+
+ xml_string = minidom.parseString(xml_string).toprettyxml(encoding="UTF-8")
+
+ if type(xml_string) is bytes:
+ xml_string = xml_string.decode("utf-8")
+
+ self.view.replace(edit, selection, xml_string)
+
+ if selected_entire_file:
+ self.change_syntax()
+
+ except Exception:
+ exc = sys.exc_info()[1]
+ self.highlight_error(str(exc))
+ sublime.status_message(str(exc))
+
+ def traverse(self, el, ha):
+ if type(ha) is dict and ha.keys():
+ for i in ha.keys():
+ e = ET.Element(i)
+ el.append(self.traverse(e, ha[i]))
+ elif type(ha) is list:
+ e_items = ET.Element('items')
+ for i in ha:
+ e_items.append(self.traverse(ET.Element('item'), i))
+ el.append(e_items)
+ else:
+ el.set('value', str(ha))
+
+ return el
+
+ def change_syntax(self):
+ self.view.set_syntax_file("Packages/XML/XML.tmLanguage")
+
+
class JqPrettyJsonOut(sublime_plugin.TextCommand):
def run(self, edit, jq_output=''):
self.view.insert(edit, 0, jq_output)
@@ -175,4 +242,4 @@ class JqPrettyJsonOut(sublime_plugin.TextCommand):
def plugin_loaded():
global s
- s = sublime.load_settings("Pretty JSON.sublime-settings")
\ No newline at end of file
+ s = sublime.load_settings("Pretty JSON.sublime-settings")
diff --git a/README.md b/README.md
index 3014f05..edabfcd 100644
--- a/README.md
+++ b/README.md
@@ -29,6 +29,10 @@ If JSON is not valid it will be displayed in status bar of Sublime Text
Using Command Palette Ctrl+Shift+P find "Pretty JSON: Minify (compress) JSON" this will make selection or full buffer as single line JSON which later you can use in command lines (curl/httpie) or somewhere else...
+### Convert JSON to XML
+
+Using Command Palette Ctrl+Shift+P search fo "Pretty JSON: JSON 2 XML" this will convert your selected JSON of full buffer to XML and replace syntax and buffer to XML output
+
## ./jQ query/filter usage
Demo: