4 Replies Latest reply on Mar 7, 2014 8:20 AM by gimbal2

    How to escape  unwanted special chatacters in string data while converting into xml data


      Hi All,

           I'am trying to converting string data into xml data using xml beans and StringEscapeUtils.This is work fine but in one case it if the data contains special charactes.


      Code snippet


      import org.apache.commons.lang3.StringEscapeUtils;

      import org.apache.xmlbeans.XmlException;

      import org.apache.xmlbeans.XmlObject;



      public class ParseXMLData {





          public static XmlObject parseXML(String stringXML) {

              XmlObject xmlObject = null;

              try {

                  String data = StringEscapeUtils.unescapeXml(stringXML);






                  xmlObject = XmlObject.Factory.parse(data);

                  // System.out.println(xmlObject);



              } catch (Exception e) {



              return xmlObject;




      success case


      i/p------&lt;aaa&gt;<bbb>This converts string to xml</bbb>&lt;/aaa&gt;

      o/p---<aaa><bbb>This converts string to xml</bbb></aaa>


      Failer case


      i/p----&lt;aaa&gt;<bbb>This fails if it contains &lt; symbols</bbb>&lt;/aaa&gt;

      expected o/p----<aaa><bbb>This fails if it contains &lt; symbols</bbb></aaa>


      Observed that it converts &lt; to '<' but  as per xml rules, if data contains '< ' it fails.

      I want to convert staring and end tags to xml format and if data in b/w middle of starting and ending tags do'n need to convert it.

      Can annybody helps how to do it.