formatting

Scala: Displaying XML in a human-readable format (pretty printing)

Problem: You have some XML in a hard-to-read format in a Scala application, and want to print it in a format that’s easier to read, at least for humans.

Solution

Use the scala.xml.PrettyPrinter class. To see how it works, imagine starting with a long, continuous string of XML:

Drupal 6 - The CKEditor is removing/deleting CODE tags

I had a problem using the CKEditor with Drupal 6 where the CKEditor would not display <code> tags properly in the editor, and would then delete trailing spaces after the <code> tag. After some digging around, I finally found that I needed to comment out the following line in the ckeditor.config.js of my CKEditor module installation:

Android JSON - Print a JSON string in a human-readable format (for debugging)

Android JSON tip: How to use the JSONObject toString(n) method to debug a JSON string in an Android app, by printing the string in a human-readable format.

While working on an Android project recently, I noticed there is a toString method on a JSONObject object that looks like this:

The Scala String format approach (and Java String.format)

Scala String formatting FAQ: How do I write code that is equivalent to the Java String.format class? That is, how do I format strings like that in Scala?

NOTE: As of Scala 2.10 this approach is a little out of date. You can still use this approach, but there's a better way to handle this situation in Scala 2.10.

In Java I always write code like this to format a String:

A Log4J format example

Log4J formatting FAQ: Can you share a Log4J output/logging format example?

Sure. I'll share a Java Log4J format example that I'm pretty happy with.

I've used the following Log4J logging format quite a bit lately, as I've been working on a headless Java app that can be deployed on thousands of computers, and I was looking for a good Log4J format that was easily readable by humans, and also easy to parse by computers. Here's what my Log4J output format look like these days:

Ant - How to use a date or timestamp in an Ant build script

Summary: An Ant date and timestamp (tstamp) task example.

I was just digging through some Ant build scripts I've created, and I noticed a segment of a build script that first creates a timestamp, and then uses that timestamp in the process of creating a manifest file. (This build script is used for building a Java Swing application.)

Here's the code from my Ant script that does this timestamp magic:

Perl printf format examples (reference page)

Here's a reference page (cheat sheet) of Perl printf formatting options. Hopefully this list covers the most common printf printing options you'll run into, or will at least point you in the right direction.

Formatting strings

Here are several examples that show how to print strings (text) with Perl and printf. I'll use single quotes in all my printf examples to help demonstrate left- and right-justification.

Java printf - output format examples with System.out.format

In my previous tutorial on using the Java String format method ("Java sprintf") I showed how to use the format method of the Java String class to format strings and text output. That method works fine in many situations where you use the sprintf function in other languages, such as when you need to either concatenate strings, or print formatted output using something like Log4J.

Syndicate content