4 Replies Latest reply: Mar 7, 2014 2:20 AM by gimbal2 RSS

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

    Mani

      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);

                  System.out.println(data);

       

       

       

       

                  xmlObject = XmlObject.Factory.parse(data);

                  // System.out.println(xmlObject);

       

       

              } catch (Exception e) {

                  e.printStackTrace();

              }

              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.

       

      Regards,

      Mani