Use JAXB (xjc) generated classes in android Use JAXB (xjc) generated classes in android xml xml

Use JAXB (xjc) generated classes in android


I had to do some extra researching to make cathixx's answer work since I'm new to Ant, so I'll share this to help others.

These instructions will take Java files with code like:

import javax.xml.bind.annotation.XmlElement;@XmlRootElementpublic class Response {...

...and comment these occurrences out, so it looks like:

/*import javax.xml.bind.annotation.XmlElement;*//*@XmlRootElement*/public class Response {...

First, create a file build.xml (or whatever you want to call it - must be .xml) in a new Eclipse project (a "General" project is fine).

Then, add the following text to the build.xml file:

<?xml version="1.0"?><project    name="CommentOutXmlAnnotations"    basedir="."    default="commentOutXmlAnnotations" ><!-- This Ant script comments out the following lines from the Java files in this directory:    import javax.xml.bind.annotation.*;    @Xml* -->    <target        name="commentOutXmlAnnotations"                description="Run" >            <replaceregexp                byline="false"                flags="g" >                <regexp pattern="(@Xml[A-Za-z0-9]+(\([^)]+\))?|import javax\.xml\.bind\.annotation\.[A-Za-z0-9.]+;)[ \t]*(\r?\n)" />                <substitution expression="/*\1*/\3" />                <fileset dir="." >                    <include name="*.java" />                </fileset>            </replaceregexp>            </target> </project>

Put the *.java files you want to comment out the XML imports and annotations for in the same directory as the build.xml file.

Right-click on the build.xml file in Eclipse, and click "Run As->Ant Build".

You should see output like:

Buildfile: D:\Eclipse_Projects\StripAnnotations\build.xmlcommentOutXmlAnnotations:BUILD SUCCESSFULTotal time: 403 milliseconds

...and the XML imports and annotations should be commented out of your files.

Done!

Note: if you want to include all *.java files in all subdirectories of the build.xml file (for example, to comment out all XML annotations/imports generated for a bunch of JAXB classes in multiple packages), change the fileset tag to:

<fileset dir="." >    <include name="**/*.java" /></fileset>


now, I solved it by myself by commenting all annotations with the following ant script:

<replaceregexp flags="g" byline="false">  <regexp pattern="(@Xml[A-Za-z0-9]+(\([^)]+\))?|import javax\.xml\.bind\.annotation\.[A-Za-z0-9.]+;)[ \t]*(\r?\n)"/>  <substitution expression="/*\1*/\3"/>  <fileset dir="path/to/files">    <include name="*.java"/>  </fileset></replaceregexp>