Java examples for XML:XML String
Appends an empty XML tag to the given StringBuffer .
/******************************************************************************* * Copyright (c) 2014 Karlsruhe Institute of Technology, Germany * Technical University Darmstadt, Germany * Chalmers University of Technology, Sweden * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors://from ww w .j a va 2s . c om * Technical University Darmstadt - initial API and implementation and/or initial documentation *******************************************************************************/ import java.util.Map; import java.util.Map.Entry; public class Main{ /** * The used leading white space in each level. */ public static final String LEADING_WHITE_SPACE_PER_LEVEL = " "; /** * Appends an empty tag to the given {@link StringBuffer}. * @param level The level. * @param tagName The tag name. * @param attributeValues The attributes. * @param sb The {@link StringBuffer} to append to. */ public static void appendEmptyTag(int level, String tagName, Map<String, String> attributeValues, StringBuffer sb) { appendWhiteSpace(level, sb); sb.append("<"); sb.append(tagName); for (Entry<String, String> entry : attributeValues.entrySet()) { appendAttribute(entry.getKey(), entry.getValue(), sb); } sb.append("/>"); appendNewLine(sb); } /** * Adds leading white space to the {@link StringBuffer}. * @param level The level in the tree used for leading white space (formating). * @param sb The {@link StringBuffer} to write to. */ public static void appendWhiteSpace(int level, StringBuffer sb) { for (int i = 0; i < level; i++) { sb.append(LEADING_WHITE_SPACE_PER_LEVEL); } } /** * Adds an XML attribute to the given {@link StringBuffer}. * @param attributeName The attribute name. * @param value The attribute value. * @param sb The {@link StringBuffer} to write to. */ public static void appendAttribute(String attributeName, String value, StringBuffer sb) { if (attributeName != null && value != null) { sb.append(" "); sb.append(attributeName); sb.append("=\""); sb.append(XMLUtil.encodeText(value)); sb.append("\""); } } /** * Adds a line break to the given {@link StringBuffer}. * @param sb The {@link StringBuffer} to write to. */ public static void appendNewLine(StringBuffer sb) { sb.append(StringUtil.NEW_LINE); } /** * <p> * Encodes the given text in a way that it contains no XML elements * and can be used for instance as plain text or attribute value. * </p> * <p> * The following signs are replaced: * <pre> * " => "quot; * & => "amp; * ' => "apos; * < => "lt; * > => "gt; * </pre> * </p> * @param text The text to encode. * @return The encoded text. */ public static String encodeText(String text) { if (text != null) { char[] signs = text.toCharArray(); StringBuffer sb = new StringBuffer(); for (char sign : signs) { switch (sign) { case '"': sb.append("""); break; case '&': sb.append("&"); break; case '\'': sb.append("'"); break; case '<': sb.append("<"); break; case '>': sb.append(">"); break; default: sb.append(sign); break; } } return sb.toString(); } else { return null; } } }