Java XML String Create toXmlString(String s)

Here you can find the source of toXmlString(String s)

Description

to Xml String

License

Open Source License

Parameter

Parameter Description
x a parameter

Return

String

Declaration

public static String toXmlString(String s) 

Method Source Code

//package com.java2s;
/*/*from  w  ww  .ja v a2  s. c  o  m*/
 * $RCSfile: StringUtil,v $$
 * $Revision: 1.0  $
 * $Date: 2011  $
 *
 * Copyright (C) 2011 GyTech, Inc. All rights reserved.
 *
 * This software is the proprietary information of GyTech, Inc.
 * Use is subject to license terms.
 */

public class Main {
    public static final String Empty = "";

    /**
    * @param x
    * @return String
    */
    public static String toXmlString(String s) {
        return toXmlString(s, Empty);
    }

    /**
    * <p>!@#$%^&amp;*()_+|</p>
    * <p>:&quot;?&gt;&lt;~`*</p>
    * @param x
    * @return String
    * 
    * @author lizj
    */
    public static String toXmlString(String s, String defaultValue) {
        if (s == null) {
            return defaultValue;
        }

        char c;

        StringBuilder buf = new StringBuilder();

        for (int i = 0, size = s.length(); i < size; i++) {
            c = s.charAt(i);

            switch (c) {
            case '&': {
                buf.append("&amp;");
                break;
            }
            case '"': {
                buf.append("&quot;");
                break;
            }
            case '<': {
                buf.append("&lt;");
                break;
            }
            case '>': {
                buf.append("&gt;");
                break;
            }
            default: {
                buf.append(c);
                break;
            }

            }
        }

        return buf.toString();
    }

    /**
    * @param b
    * @return String
    * @author: lizj
    */
    public static String toString(boolean b) {
        return Boolean.toString(b);
    }

    /**
    * @param b
    * @return String
    * @author: lizj
    */
    public static String toString(byte b) {
        return Byte.toString(b);
    }

    /**
    * @param b
    * @return String
    * @author: lizj
    */
    public static String toString(char c) {
        return Character.toString(c);
    }

    /**
    * @param s
    * @return String
    * @author: lizj
    */
    public static String toString(short s) {
        return Short.toString(s);
    }

    /**
    * @param i
    * @return String
    * @author: lizj
    */
    public static String toString(int i) {
        return Integer.toString(i);
    }

    /**
    * @param f
    * @return String
    * @author: lizj
    */
    public static String toString(float f) {
        return Float.toString(f);
    }

    /**
    * @param d
    * @return String
    * @author: lizj
    */
    public static String toString(double d) {
        return Double.toString(d);
    }

    /**
    * @param l
    * @return String
    * @author: lizj
    */
    public static String toString(long l) {
        return Long.toString(l);
    }

    /**
    * @param b
    * @return
    * @author: lizj
    */
    public static String toString(Boolean b) {
        return b != null ? b.toString() : Empty;
    }

    /**
    * @param b
    * @return String
    * @author: lizj
    */
    public static String toString(Byte b) {
        return b != null ? b.toString() : Empty;
    }

    /**
    * @param s
    * @return String
    * @author: lizj
    */
    public static String toString(Short s) {
        return s != null ? s.toString() : Empty;
    }

    /**
    * @param c
    * @return String
    * @author: lizj
    */
    public static String toString(Character c) {
        return c != null ? c.toString() : Empty;
    }

    /**
    * @param i
    * @return String
    * @author: lizj
    */
    public static String toString(Integer i) {
        return i != null ? i.toString() : Empty;
    }

    /**
    * @param f
    * @return String
    * @author: lizj
    */
    public static String toString(Float f) {
        return f != null ? f.toString() : Empty;
    }

    /**
    * @param d
    * @return String
    * @author: lizj
    */
    public static String toString(Double d) {
        return d != null ? d.toString() : Empty;
    }

    /**
    * @param l
    * @return String
    * @author: lizj
    */
    public static String toString(Long l) {
        return l != null ? l.toString() : Empty;
    }

    /**
    * @param s
    * @return String
    * @author: lizj
    */
    public static String toString(String s) {
        return s != null ? s : Empty;
    }

    /**
    * @param buf
    * @return String
    * @author: lizj
    */
    public static String toString(StringBuffer buf) {
        return buf != null ? buf.toString() : Empty;
    }

    /**
    * @param b
    * @param defaultValue
    * @return String
    */
    public static String toString(boolean b, String defaultValue) {
        return Boolean.toString(b);
    }

    /**
    * @param b
    * @param defaultValue
    * @return String
    * @author: lizj 
    */
    public static String toString(byte b, String defaultValue) {
        return Byte.toString(b);
    }

    /**
    * @param b
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(char c, String defaultValue) {
        return Character.toString(c);
    }

    /**
    * @param s
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(short s, String defaultValue) {
        return Short.toString(s);
    }

    /**
    * @param i
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(int i, String defaultValue) {
        return Integer.toString(i);
    }

    /**
    * @param f
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(float f, String defaultValue) {
        return Float.toString(f);
    }

    /**
    * @param d
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(double d, String defaultValue) {
        return Double.toString(d);
    }

    /**
    * @param l
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(long l, String defaultValue) {
        return Long.toString(l);
    }

    /**
    * @param b
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(Boolean b, String defaultValue) {
        return (b != null ? b.toString() : defaultValue);
    }

    /**
    * @param b
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(Byte b, String defaultValue) {
        return (b != null ? b.toString() : defaultValue);
    }

    /**
    * @param s
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(Short s, String defaultValue) {
        return (s != null ? s.toString() : defaultValue);
    }

    /**
    * @param c
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(Character c, String defaultValue) {
        return (c != null ? c.toString() : defaultValue);
    }

    /**
    * @param i
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(Integer i, String defaultValue) {
        return (i != null ? i.toString() : defaultValue);
    }

    /**
    * @param f
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(Float f, String defaultValue) {
        return (f != null ? f.toString() : defaultValue);
    }

    /**
    * @param d
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(Double d, String defaultValue) {
        return (d != null ? d.toString() : defaultValue);
    }

    /**
    * @param l
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(Long l, String defaultValue) {
        return (l != null ? l.toString() : defaultValue);
    }

    /**
    * @param s
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(String s, String defaultValue) {
        return (s != null ? s : defaultValue);
    }

    /**
    * @param buf
    * @param defaultValue
    * @return String
    * @author: lizj
    */
    public static String toString(StringBuffer buf, String defaultValue) {
        return (buf != null ? buf.toString() : defaultValue);
    }
}

Related

  1. toXMLString(final boolean[] array)
  2. toXmlString(float value)
  3. toXMLString(String in)
  4. toXMLString(String javaString)
  5. toXMLString(String org)
  6. toXMLString(String str)
  7. toXMLString(String string)
  8. toXMLString(String t)
  9. toXMLString(String text)