Groovy and Java code parsing which are embedded inside a xml tag

161 Views Asked by At

I have a xml file which has Java and groovy codes in some tags and I need to format the code, I am able to format xml but not java and groovy code inside xml tags.

Ex: Input:

<?xml version="1.0" encoding="UTF-8"?>
<config  xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" charset="UTF-8" scriptlang="groovy">

<script><![CDATA[

  class Room{
   public int  length=""; public List<String> comments = new ArrayList<>();static void main(String[] args){}
  }
  
 ]]></script>
 <export include-original-data="true">
  
 </export>
</config>

I need this as Output(well formatted):

<?xml version="1.0" encoding="UTF-8"?>
<config  xmlns="http://web-harvest.sourceforge.net/schema/1.0/config" charset="UTF-8" scriptlang="groovy">
 <script><![CDATA[
  class Room{
            public int  length=""; 
     public List<String> comments = new ArrayList<>();
     static void main(String[] args){}
  ]]>
 </script>
 <export include-original-data="true">
 </export>
</config>

I can get inner-text of xml tag and can format, but I need to do formatting without getting inner text

I used this code for xml formatting

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    Document doc = db.parse(new InputSource(new StringReader(xml)));

    OutputFormat format = new OutputFormat(doc);
    format.setIndenting(true);
    format.setIndent(4);
    format.setOmitXMLDeclaration(ommitXmlDeclaration);
    format.setLineWidth(Integer.MAX_VALUE);
    Writer outxml = new StringWriter();
    XMLSerializer serializer = new XMLSerializer(outxml, format);
    serializer.serialize(doc);
0

There are 0 best solutions below