使用JAXB(XJC)Android中生成的类 [英] Use JAXB (xjc) generated classes in android

查看:712
本文介绍了使用JAXB(XJC)Android中生成的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在安卓使用 REST的web服务。该 Web服务可以处理 JSON XML ,该API被描述为 XSD 。所以我用 JAXB 生成从 XSD 类,然后我用杰克逊 JSON 处理器生成 JSON 从我的实例。

I am using a Rest-Webservice in Android. The Web Service could handle JSON and XML, the API is described as an XSD. So I used JAXB to generate classes from the XSD and then I used jackson JSON processor to generate JSON from my instances.

现在的问题是,该 JAXB XJC )生成的类与 JAXB 注释和安卓不能处理这些。我试图在的JAXB api.jar和添加到我的Andr​​oid项目,但在Dalvik不会用核心类。

The problem is, that JAXB (xjc) generates classes with JAXB annotations and Android can't handle those. I tried to add the jaxb-api.jar to my android project but the Dalvik won't use core classes.

有关我的第一个执行我手动删除批注。但现在的 XSD 更新了,我不想这样每次发生这种情况的时间去做。

For my first implementation I manually removed the annotations. But now the XSD was updated and I don't want to do this every time this happens.

你有任何想法如何以更好的方式来处理这个问题?是否有可能使用 JAXB / XJC 没有标注,或有另一种code generater,可以做到这一点?你知道一个简单的方法来移除Java类的注释(即使它们都打印在多行)?是否有一个项目设置为安卓 的Eclipse 的项目,这使得在Dalvik让核心库?

Do you have any idea how to handle this problem in a better way? Is there a possibility to use jaxb/xjc without annotations or is there another code generater that could do this? Do you know an easy way to remove annotations from java classes (even if they are printed in multiple lines)? Is there a project setting for Android Eclipse projects, that makes the dalvik to allow core libs?

THX,cathixx

thx, cathixx

推荐答案

我不得不做一些额外的研究,使cathixx的答复工作,因为我是新来的蚂蚁,所以我会分享这个帮助别人。

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.

这些指令需要的Java文件code这样的:

These instructions will take Java files with code like:

import javax.xml.bind.annotation.XmlElement;

@XmlRootElement
public class Response {...

...和评论这些事件出来,所以它看起来像:

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

/*import javax.xml.bind.annotation.XmlElement;*/

/*@XmlRootElement*/
public class Response {...

首先,创建一个文件的build.xml (或任何你想称呼它 - 必须的.xml)的一个新的Eclipse项目(一个常规项目精)。

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

然后,下面的文本添加到的build.xml 文件:

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>

*。java的文件要注释掉XML导入和注释在同一目录中的build.xml文件。

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

在Eclipse中的build.xml文件上单击右键,然后单击运行AS-> Ant构建。

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.xml
commentOutXmlAnnotations:
BUILD SUCCESSFUL
Total time: 403 milliseconds

...和XML导入和注释要注释您的文件。

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

完成!

请注意:如果要包含在build.xml文件的所有子目录中的所有 *的Java 文件(例如,注释掉所有的XML注释/进口产生让一堆JAXB类多包),在文件集标签更改为:

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>

这篇关于使用JAXB(XJC)Android中生成的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆