Java Utililty Methods XML Encode

List of utility methods to do XML Encode

  1. HOME
  2. Java
  3. X
  4. XML Encode

Description

The list of methods to do XML Encode are organized into topic(s).

Method

String encodeXML(String text)
Escape characters for text appearing as XML data, between tags.
if (text == null) {
 return null;
final StringBuilder result = new StringBuilder();
final StringCharacterIterator iterator = new StringCharacterIterator(text);
char character = iterator.current();
while (character != CharacterIterator.DONE) {
 if (character == '<') {
...
String textToXml(String text)
Given text strings containing xml reserved characters, replace with valid xml representation characters > => & gt; < => & lt; & => & amp; ' => & apos; " => & quot;
if (text == null || text.equals("")) {
 return "";
String str = text;
str = str.replaceAll("&", "&");
str = str.replaceAll(">", ">");
str = str.replaceAll("<", "<");
str = str.replaceAll("'", "'");
...
String textToXML(String text)
Converts a text string to XML entities.
return textToHTML(text, true);
String toXMLCData(String cdata)
to XMLC Data
char[] charArray = cdata.toCharArray();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < charArray.length; i++) {
 final char ch = charArray[i];
 switch (cdataFilter(ch)) {
 case 0:
 buffer.append(ch);
 break;
...
String toXMLCharData(String javaString)
to XML Char Data
char[] charArray = javaString.toCharArray();
StringBuilder buffer = new StringBuilder();
for (int i = 0; i < charArray.length; i++) {
 final char ch = charArray[i];
 switch (ch) {
 case ' ':
 buffer.append(' ');
 break;
...
String toXMLEscapedTextExpandingWhitespace(String text)
to XML Escaped Text Expanding Whitespace
text = text.replaceAll("\t", " ");
final int len = text.length();
final StringBuilder result = new StringBuilder(len);
char myChar;
for (int i = 0; i < len; ++i) {
 myChar = text.charAt(i);
 switch (myChar) {
 case '&':
...
String unwrapCdata(String s)
unwrap Cdata
return (s.startsWith("<![CDATA[") && s.endsWith("]]>")
 ? s.substring(9, s.length() - 3).replace("]]]]><![CDATA[>", "]]>")
 : s);
String unwrapCdata(String sText)
If the text is wrapped with a "[CDATA[ ]]" tag then it is removed and the text inside is returned.
if ((sText != null) && (sText.trim().startsWith("[CDATA[")) && (sText.trim().endsWith("]]"))) {
 return sText.trim().substring(7, sText.trim().length() - 2);
} else {
 return sText;
String unwrapCDATA(String str)
unwrap CDATA
return str.replace(CDATA_START, "").replace(CDATA_END, "");
String xmlEncode(final String text)
xml Encode
StringBuilder result = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
 char currentChar = text.charAt(i);
 if (!((currentChar >= 'a' && currentChar <= 'z') || (currentChar >= 'A' && currentChar <= 'Z')
 || (currentChar >= '0' && currentChar <= '9'))) {
 result.append("&#" + (int) currentChar + ";");
 } else {
 result.append(currentChar);
...


AltStyle によって変換されたページ (->オリジナル) /