Java XML Escape escape4XML(long study_id, String xmlStr)

Here you can find the source of escape4XML(long study_id, String xmlStr)

Description

escape XML

License

Open Source License

Declaration

public static String escape4XML(long study_id, String xmlStr) 

Method Source Code


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

import java.text.CharacterIterator;

import java.text.StringCharacterIterator;

public class Main {
    public static String escape4XML(long study_id, String xmlStr) {
        if (xmlStr == null)
            return null;
        StringBuilder result = new StringBuilder();
        StringCharacterIterator sci = new StringCharacterIterator(xmlStr);
        char c = sci.current();
        while (c != CharacterIterator.DONE) {
            if (c == '<') {
                result.append("&lt;");
            } else if (c == '>') {
                result.append("&gt;");
            } else if (c == '\"') {
                result.append("&quot;");
            } else if (c == '\'') {
                result.append("&#039;");
            } else if (c == '&') {
                result.append("&amp;");
            } else {
                if (!Character.isDefined(c)) {
                    try {
                        throw new Exception();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        System.err.println("study:" + study_id + " undefined charachter:" + c + " in " + xmlStr);
                    }//from  www.ja v  a  2  s . com

                }
                result.append(c);
            }
            c = sci.next();
        }
        return result.toString();
    }
}

Related

  1. escapeNonXmlTagSymbols(String string)
  2. escapeXML(String s)