通过XSD def将逻辑代码插入到生成的JAXB java文件中
问题是,出于某种原因。xsd没有/不能定义除基本属性、setter和getter之外的所有逻辑变量,因此我们尝试通过xsd定义“注入代码”,其他人实际上已经讨论过好几次了。我对“simple java method”的“simple injection”没有问题,因为它不需要在类def上使用任何“import”语句 但是如果我们想用它的话。在我看来,除了setter或getter之外,我们不可能获取或导入任何包,详情见下文通过XSD def将逻辑代码插入到生成的JAXB java文件中,java,xml,xsd,jaxb,Java,Xml,Xsd,Jaxb,问题是,出于某种原因。xsd没有/不能定义除基本属性、setter和getter之外的所有逻辑变量,因此我们尝试通过xsd定义“注入代码”,其他人实际上已经讨论过好几次了。我对“simple java method”的“simple injection”没有问题,因为它不需要在类def上使用任何“import”语句 但是如果我们想用它的话。在我看来,除了setter或getter之外,我们不可能获取或导入任何包,详情见下文 xsd定义测试.xsd <?xml versio
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://company.com/schema/response"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:test="http://company.com/schema/response"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
xmlns:ci="http://jaxb.dev.java.net/plugin/code-injector"
jaxb:extensionBindingPrefixes="ci">
<xs:element name="client">
<xs:complexType>
<xs:annotation>
<xs:appinfo>
<ci:code>
<![CDATA[
private String str;
public String returnStr() {
Locations locationCls =this.getLocations();
List<String> locationids = new ArrayList<String>();
// get a list of locationid into locationids (list)
List<Location> locationList = locationCls.getLocation();
for (Location loc : locationList) {
locationids.add(String.valueOf(loc.getId()));
}
// return string like loc1,loc2,loc3
return StringUtils.join(locationids, ',');
}
]]>
</ci:code>
</xs:appinfo>
</xs:annotation>
<xs:sequence>
<xs:element name="name" type="xs:NCName" />
<xs:element name="pass" type="xs:NCName" />
<xs:element ref="test:locations" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="locations">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="test:location" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="location">
<xs:complexType>
<xs:attribute name="id" use="required" type="xs:string" />
<xs:attribute name="address" use="required" type="xs:string" />
<xs:attribute name="biz" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:schema>
LocationId=新的ArrayList();
//将locationid列表获取到locationid(列表)中
List locationList=locationCls.getLocation();
对于(位置位置:位置列表){
add(String.valueOf(loc.getId());
}
//返回字符串,如loc1、loc2、loc3
返回StringUtils.join(locationId,,);
}
]]>
private String str;
public String returnStr() {
Locations locationCls =this.getLocations();
List<String> locationids = new ArrayList<String>();
// get a list of locationid into locationids (list)
List<Location> locationList = locationCls.getLocation();
for (Location loc : locationList) {
locationids.add(String.valueOf(loc.getId()));
}
// return string like loc1,loc2,loc3
return StringUtils.join(locationids, ',');
}
私有字符串str;
公共字符串returnStr(){
LocationLocationCLS=this.getLocations();
List locationids=new ArrayList();
//将locationid列表获取到locationid(列表)中
List locationList=locationCls.getLocation();
对于(位置位置:位置列表){
add(String.valueOf(loc.getId());
}
//返回字符串,如loc1、loc2、loc3
返回StringUtils.join(locationId,,);
}
因此,我们知道jdk会抱怨编译错误,因为ApacheCommons中的StringUtil(或其他第三部分的util工具,如google collections,用于在其他场景中提供帮助)没有导入到生成的文件中。了解一些google项目使用jaxb插件将方法插入或调用到生成的java文件中。我只想花上一天左右的时间看看我们是否可以通过xsd本身而不用任何插件。任何想法都将不胜感激。您可以在要注入的代码中指定完全分类的类名,例如:
return org.apache.commons.lang.StringUtils.join(locationids, ',');
丹尼,太好了。在某些情况下,这是回避这个问题的一种方式。在这种情况下,它应该可以正常工作。