Java tutorial
//package com.java2s; /** * Copyright (c) 2009 University of Rochester * * This program is free software; you can redistribute it and/or modify it under the terms of the MIT/X11 license. The text of the * license can be found at http://www.opensource.org/licenses/mit-license.php and copy of the license can be found on the project * website http://www.extensiblecatalog.org/. * */ public class Main { /** * Create a simple XML tag. For example xmlTag("foo", "bar") creates * <foo>bar</foo> * * @param tagName * The name of the tag * @param content * The content of the tag * @return The created XML tag */ public static String xmlTag(String tagName, String content) { return xmlTag(tagName, content, null); } /** * Create a simple XML tag with attributes. For example * xmlTag("foo", "bar", new String[]{"foo", "bar}) creates * <foo foo="bar">bar</foo> * @param tagName The name of the tag * @param content The content of the tag * @param attributes The attributes of the element * @return The created XML tag */ public static String xmlTag(String tagName, String content, String[] attributes) { StringBuffer sb = new StringBuffer(); sb.append('<').append(tagName); if (null != attributes) { for (int i = 0; i < attributes.length; i += 2) { sb.append(' ').append(attributes[i]).append("=\"").append(attributes[i + 1]).append('"'); } } if (null == content || 0 == content.length()) { sb.append(" />"); } else { sb.append('>'); sb.append(content); sb.append("</").append(tagName).append('>'); } return sb.toString(); } }