Pretty print XML in java 8

In reply to Espinosa’s comment, here is a solution when “the original xml is not already (partially) indented or contain new lines“. Background Excerpt from the article (see References below) inspiring this solution: Based on the DOM specification, whitespaces outside the tags are perfectly valid and they are properly preserved. To remove them, we can … Read more

How to Print “Pretty” String Output in Python

Standard Python string formatting may suffice. # assume that your data rows are tuples template = “{0:8}|{1:10}|{2:15}|{3:7}|{4:10}” # column widths: 8, 10, 15, 7, 10 print template.format(“CLASSID”, “DEPT”, “COURSE NUMBER”, “AREA”, “TITLE”) # header for rec in your_data_source: print template.format(*rec) Or # assume that your data rows are dicts template = “{CLASSID:8}|{DEPT:10}|{C_NUM:15}|{AREA:7}|{TITLE:10}” # same, but … Read more

Disabling sorting mechanism in pprint output

Python 3.8 or newer: Use sort_dicts=False: pprint.pprint(data, sort_dicts=False) Python 3.7 or older: You can monkey patch the pprint module. import pprint pprint.pprint({“def”:2,”ghi”:3,”abc”:1,}) pprint._sorted = lambda x:x # Or, for Python 3.7: # pprint.sorted = lambda x, key=None: x pprint.pprint({“def”:2,”ghi”:3, “abc”:1}) Since the 2nd output is essentiallly randomly sorted, your output may be different from mine: … Read more

How can I pretty-print JSON using Go?

MarshalIndent will allow you to output your JSON with indentation and spacing. For example: { “data”: 1234 } The indent argument specifies the series of characters to indent with. Thus, json.MarshalIndent(data, “”, ” “) will pretty-print using four spaces for indentation.

JavaScript: How can I generate formatted easy-to-read JSON straight from an object? [duplicate]

JSON.stringify takes more optional arguments. Try: JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, 4); // Indented 4 spaces JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, “\t”); // Indented with tab From: How can I beautify JSON programmatically? It should work in modern browsers, and it is included in json2.js if you need a fallback for browsers that don’t support the JSON helper functions. For display … Read more

How do I print out a tree structure?

The trick is to pass a string as the indent and to treat the last child specially: class Node { public void PrintPretty(string indent, bool last) { Console.Write(indent); if (last) { Console.Write(“\\-“); indent += ” “; } else { Console.Write(“|-“); indent += “| “; } Console.WriteLine(Name); for (int i = 0; i < Children.Count; i++) … Read more

Javascript: How to generate formatted easy-to-read JSON straight from an object? [duplicate]

JSON.stringify takes more optional arguments. Try: JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, 4); // Indented 4 spaces JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}, null, “\t”); // Indented with tab From: How can I beautify JSON programmatically? Should work in modern browsers, and it is included in json2.js if you need a fallback for browsers that don’t support the JSON helper functions. For display purposes, … Read more