Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

public class Main {

    public static String toSafeXMLString(String str) {
        StringBuffer safeXMLStr = new StringBuffer();

        for (int i = 0; (str != null) && (i < str.length()); i++) {
            char ch = str.charAt(i);

            if (ch == '<')
                safeXMLStr.append("&lt;");
            else if (ch == '>')
                safeXMLStr.append("&gt;");
            else if (ch == '&')
                safeXMLStr.append("&amp;");
            else if (ch == '"')
                safeXMLStr.append("&quot;");
            else {
                safeXMLStr.append(ch);
            }
        }
        return safeXMLStr.toString();
    }
}