Java examples for XML:XML String
Unescapes the String by converting XML escape sequences back into normal characters.
/**/* www. j a v a 2s . c om*/ * Turns an array of bytes into a String representing each byte as an * unsigned hex number. * <p> * Method by Santeri Paavolainen, Helsinki Finland 1996<br> * (c) Santeri Paavolainen, Helsinki Finland 1996<br> * Distributed under LGPL. * * @param bytes * an array of bytes to convert to a hex-string * @return generated hex string */ //package com.java2s; public class Main { public static void main(String[] argv) throws Exception { String string = "java2s.com"; System.out.println(unescapeFromXML(string)); } /** * Unescapes the String by converting XML escape sequences back into normal * characters. * * @param string * the string to unescape. * @return the string with appropriate characters unescaped. */ public static final String unescapeFromXML(String string) { string = replace(string, "<", "<"); string = replace(string, ">", ">"); string = replace(string, """, "\""); return replace(string, "&", "&"); } /** * Replaces all instances of oldString with newString in line. * * @param line * the String to search to perform replacements on * @param oldString * the String that should be replaced by newString * @param newString * the String that will replace all instances of oldString * * @return a String will all instances of oldString replaced by newString */ public static final String replace(String line, String oldString, String newString) { if (line == null) { return null; } int i = 0; if ((i = line.indexOf(oldString, i)) >= 0) { char[] line2 = line.toCharArray(); char[] newString2 = newString.toCharArray(); int oLength = oldString.length(); StringBuffer buf = new StringBuffer(line2.length); buf.append(line2, 0, i).append(newString2); i += oLength; int j = i; while ((i = line.indexOf(oldString, i)) > 0) { buf.append(line2, j, i - j).append(newString2); i += oLength; j = i; } buf.append(line2, j, line2.length - j); return buf.toString(); } return line; } /** * Replaces all instances of oldString with newString in line. The count * Integer is updated with number of replaces. * * @param line * the String to search to perform replacements on * @param oldString * the String that should be replaced by newString * @param newString * the String that will replace all instances of oldString * * @return a String will all instances of oldString replaced by newString */ public static final String replace(String line, String oldString, String newString, int[] count) { if (line == null) { return null; } int i = 0; if ((i = line.indexOf(oldString, i)) >= 0) { int counter = 0; counter++; char[] line2 = line.toCharArray(); char[] newString2 = newString.toCharArray(); int oLength = oldString.length(); StringBuffer buf = new StringBuffer(line2.length); buf.append(line2, 0, i).append(newString2); i += oLength; int j = i; while ((i = line.indexOf(oldString, i)) > 0) { counter++; buf.append(line2, j, i - j).append(newString2); i += oLength; j = i; } buf.append(line2, j, line2.length - j); count[0] = counter; return buf.toString(); } return line; } }