Friday, March 8, 2024

Easy Way To Create HTML/XML String

A simple way to create html tags in Java.

It won't create a tree that you can parse, but it will create the text needed. You see we'll use the StringBuilder class along with the Formatter class to accomplish this. This provides a simple way to format a string without using String.format inside of a sb.append call.

import java.util.Formatter;
import java.util.Locale;

public class LocalMe {
    public static void main(String[] args) {

        StringBuilder sb = new StringBuilder();
        Formatter formatter = new Formatter(sb, Locale.ENGLISH);
        formatter.format("<%s>%s</%s>", "greeting", "Hello World", "greeting");
        System.out.println(formatter.toString());

    }
}

No comments:

Post a Comment

Ever feel like no one is listening?

 Ever have that feeling that no one is listening to you? Yeah, that feeling. It can be a strong feeling to have, a hurtful feeling also. The...