| Refresh | Home EGTry.com

escape special character in xml to produce a well-format xml


Version tested: apache commons-lang-2.6.jar

package string;

import org.apache.commons.lang.StringEscapeUtils;

public class XMLWriteStringEscape {


	public static void main(String[] args) {
		
		System.out.println("<root name=\""+xmlEscape("<\"init\">")+"\">");
		System.out.println(xmlEscape("this is a text <body>"));
		System.out.println("</root>");


	}
	public static String xmlEscape(String value) {
		return StringEscapeUtils.escapeXml(value);
	}

}



output

<root name="<"init">">
this is a text <body>
</root>