How to pretty print XML from Java?

I have a Java String that contains XML, with no line feeds or indentations. I would like to turn it into a String with nicely formatted XML. How do I do this? String unformattedXml = “<tag><nested>hello</nested></tag>”; String formattedXml = new [UnknownClass]().format(unformattedXml); Note: My input is a String. My output is a String. (Basic) mock result: … Read more

How to turn off the Eclipse code formatter for certain sections of Java code?

I’ve got some Java code with SQL statements written as Java strings (please no OR/M flamewars, the embedded SQL is what it is – not my decision). I’ve broken the SQL statements semantically into several concatenated strings over several lines of code for ease of maintenance. So instead of something like: String query = “SELECT … Read more

Pretty-Printing JSON with PHP

I’m building a PHP script that feeds JSON data to another script. My script builds data into a large associative array, and then outputs the data using json_encode. Here is an example script: $data = array(‘a’ => ‘apple’, ‘b’ => ‘banana’, ‘c’ => ‘catnip’); header(‘Content-type: text/javascript’); echo json_encode($data); The above code yields the following output: … Read more

pretty-print JSON using JavaScript

How can I display JSON in an easy-to-read (for human readers) format? I’m looking primarily for indentation and whitespace, with perhaps even colors / font-styles / etc. 2 29 Pretty-printing is implemented natively in JSON.stringify(). The third argument enables pretty printing and sets the spacing to use: var str = JSON.stringify(obj, null, 2); // spacing … Read more

Printing out a linked list using toString

public static void main(String[] args) { LinkedList list = new LinkedList(); list.insertFront(1); list.insertFront(2); list.insertFront(3); System.out.println(list.toString()); } String toString() { String result = “”; LinkedListNode current = head; while(current.getNext() != null){ result += current.getData(); if(current.getNext() != null){ result += “, “; } current = current.getNext(); } return “List: ” + result; }