我的xsd看起来像这样:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
...
<xs:element name="person">
<xs:complexType>
...
<xs:attribute name="first_name" use="optional" type="xs:string"/>
</xs:complexType>
</xs:element>
...
</xs:schema>
我编写了gradle脚本来生成代理java类。一切正常!
现在,我想在生成代理类时自动添加以下注解(我已经编写了adapter以及如何将其添加到具体字段中)。
@XmlJavaTypeAdapter(value=StringHashFunctionAdapter.class, type=String.class)
解决方案:所以我写了jaxb绑定:
<bindings
xmlns="http://java.sun.com/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchem"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:retep="http://retep.org/xml/ns/retepTools"
version="2.0">
<bindings schemaLocation="XMLreq.xsd" node="/xs:element[@name='person']/xs:complexType/xs:attribute[@name='first_name']" >
<property>
<baseType>
<xjc:javaType adapter="org.example.StringHashFunctionAdapter"/>
</baseType>
</property>
</bindings>
</bindings>
我认为这种装订是正确的。但我有以下错误:
[ERROR] [org.gradle.api.internal.project.ant.AntLoggingAdapter] [ant:xjc] [ERROR] XPath evaluation of "/xs:element[@name='person']/xs:complexType/xs:attribute[@name='first_name']" results in empty target node
这是grad尔的剧本:
dependencies {
xsd2java "com.sun.xml.bind:jaxb-xjc:2.2.6"
xsd2java "com.sun.xml.bind:jaxb-impl:2.2.6"
}
task xsd2java() {
doLast {
jaxbTargetDir.mkdirs()
ant.taskdef(name: 'xjc', classname: 'com.sun.tools.xjc.XJCTask', classpath: configurations.xsd2java.asPath)
ant.jaxbTargetDir = jaxbTargetDir
ant.xjc(
destdir: '${jaxbTargetDir}',
package: 'org.example.request',
binding:'src/main/resources/bindings.jxb',
schema: 'src/main/resources/XMLreq.xsd'
)
}
}
更新1。
我改变了路径,但我有同样的错误。
更新2
我已经修好了xmlns:xs and 我还补充说xmlns:jxb. 最终xml:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<bindings schemaLocation="XMLreq.xsd" node="/xs:schema/xs:element[@name='person']/xs:complexType/xs:attribute[@name='first_name']" >
<xjc:javaType adapter="org.example.StringHashFunctionAdapter" name="java.lang.String" />
</bindings>
</jxb:bindings
1条答案
按热度按时间3pmvbmvn1#
尝试:
/xs:schema/xs:element[@name='person']/xs:complexType/xs:attribute[@name='first_name']
xpath表达式应用于架构文档的根。你失踪了xs:schema
.