简体   繁体   English

基于XSD文件的JAXB Java代码生成

[英]JAXB java code generation base on XSD file

I'm having a problem generating java source code based on XSD file. 我在基于XSD文件生成Java源代码时遇到问题。

Please notice that the XSD file was generated based on XML example. 请注意,XSD文件是基于XML示例生成的。

XML Example XML范例

<resposta_importacao>
  <RetornoEnvio xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <num_apolice xmlns="compuletra">202476600001</num_apolice>
    <cod_retorno xmlns="compuletra">651</cod_retorno>
    <mensagem xmlns="compuletra">Erro de layout de dadosobrigat&#195;&#179;rios: cod_cobertura</mensagem>
    <id_validacao xmlns="compuletra">0</id_validacao>
  </RetornoEnvio>
  <RetornoEnvio xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <num_apolice xmlns="compuletra">202476600002</num_apolice>
    <cod_retorno xmlns="compuletra">651</cod_retorno>
    <mensagem xmlns="compuletra">Erro de layout de dados obrigat&#195;&#179;rios: cod_cobertura</mensagem>
    <id_validacao xmlns="compuletra">0</id_validacao>
  </RetornoEnvio>
</resposta_importacao>

Generated XSD 生成的XSD

<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:complexType name="RetornoEnvio">
        <xsd:sequence>
            <xsd:element name="num_apolice" type="xsd:integer" xmlns="compuletra" />
            <xsd:element name="cod_retorno" type="xsd:int" xmlns="compuletra" />
            <xsd:element name="mensagem" type="xsd:string" xmlns="compuletra" />
            <xsd:element name="id_validacao" type="xsd:int" xmlns="compuletra" />
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="resposta_importacao">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element maxOccurs="unbounded" type="RetornoEnvio" name="RetornoEnvio" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>

Generated Java Class 生成的Java类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "RetornoEnvio", propOrder = {
    "numApolice",
    "codRetorno",
    "mensagem",
    "idValidacao"
})
public class RetornoEnvio {

    @XmlElement(name = "num_apolice", required = true)
    protected BigInteger numApolice;
    @XmlElement(name = "cod_retorno")
    protected int codRetorno;
    @XmlElement(required = true)
    protected String mensagem;
    @XmlElement(name = "id_validacao")
    protected int idValidacao;

The problem here is that the XML has the 这里的问题是XML具有

xmlns="compuletra" 的xmlns = “compuletra”

But the java class don't. 但是java类没有。

I know I can just insert the namespace attribute to the XmlElement annotation but it would be nicer if the code generation could insert it for me. 我知道我可以将namespace属性插入XmlElement批注中,但是如果代码生成可以为我插入它,那就更好了。

Thank you 谢谢

The xmlns attribute in the element defintion in the XML Schema has nothing to do with the namespace qualification of the element. XML模式中元素定义中的xmlns属性与元素的名称空间限定无关。 This is why JAXB isn't doing anything with it. 这就是JAXB不对其执行任何操作的原因。

<xsd:element name="cod_retorno" type="xsd:int" xmlns="compuletra" />

Well, just to answer mine own question, so that it does not go unanswered.. 好吧,只回答我自己的问题,这样就不会无解。

I could achieve this namespace difference with the most obvious solution, after thinking of it a little more. 在考虑了一点之后,我可以通过最明显的解决方案来实现这种名称空间差异。 Basically using two XSD files with two different namespaces and importing them in a third XSD file. 基本上使用两个具有两个不同命名空间的XSD文件,并将它们导入第三个XSD文件。

It all worked as expected. 一切都按预期进行。

Thank you, 谢谢,

Thiago 蒂亚戈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM