Java Encode encodeXMLString(String aString, PrintWriter aWriter)

Here you can find the source of encodeXMLString(String aString, PrintWriter aWriter)

Description

encode XML String

License

Open Source License

Declaration

public static final void encodeXMLString(String aString, PrintWriter aWriter) 

Method Source Code


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

import java.io.PrintWriter;

public class Main {
    public static final void encodeXMLString(String aString, PrintWriter aWriter) {
        int len = aString.length();
        for (int i = 0; i < len; i++) {
            char ch = aString.charAt(i);
            switch (ch) {
            case '&':
                aWriter.print("&amp;");
                break;
            case '>':
                aWriter.print("&gt;");
                break;
            case '<':
                aWriter.print("&lt;");
                break;
            case '\'':
                aWriter.print("&apos;");
                break;
            case '"':
                aWriter.print("&quot;");
                break;
            default:
                if (ch < ' ' || ch > '~') {
                    aWriter.print("&#");
                    aWriter.print((int) ch);
                    aWriter.print(';');
                } else {
                    aWriter.print(ch);//from w ww  .  ja  va2 s .c o m
                }
            }
        }
    }
}

Related

  1. encodeUtf8AsBase64(String data)
  2. encodeVarUInt32(int unsignedValue, OutputStream outputStream)
  3. encodeVarUInt64(long unsignedValue, OutputStream outputStream)
  4. encodeWikiTag(String s)
  5. encodeXML(String input)