简体   繁体   English

如何使用通用类从Xsd和Maven生成JAXB

[英]How to generate JAXB from xsd with maven using common classes

I am trying to parse .xml files that have the same structure but have different names. 我正在尝试解析具有相同结构但名称不同的.xml文件。 I am using maven to auto generate java classes from .xsd files to parse the xml files. 我正在使用Maven从.xsd文件自动生成Java类来解析xml文件。 I need to be able to auto generate most of the classes but use some pre created classes. 我需要能够自动生成大多数类,但要使用一些预先创建的类。 This will allow me to extract some business logic and avoid duplication. 这将使我能够提取一些业务逻辑并避免重复。 Consider the two xml examples below: 考虑下面的两个xml示例:

<x>
    <y>
        <d attribute="value1"/>
    </y>
    <y>
        <d attribute="value2"/>
    </y>
</x>


<b>
    <c>
        <d attribute="value1"/>
    </c>
    <c>
       <d attribute="value2"/>
    </c>
</b>

The nodes "y" and "c" have identical structure but different names. 节点“ y”和“ c”具有相同的结构,但名称不同。 I would like to be able to create a different class "W" that could be used to parse both xml files. 我希望能够创建一个不同的类“ W”,该类可用于解析两个xml文件。 Then when the Maven auto generation happens instead of creating the classes "Y" and "C" it will use my class "W" and apply it to both nodes. 然后,当发生Maven自动生成而不是创建类“ Y”和“ C”时,它将使用我的类“ W”并将其应用于两个节点。 This can be accomplished manually by creating classes that have a structure similar to this: 可以通过创建具有类似于以下结构的类来手动完成此操作:

public class x {
    @XmlType("y")
    private List<W> w;
}

public class W {
    private D d;
}

public class B {
    @XmlType("c")
    private List<W> w;
}

There are about 10 .xsd files each with 10k-20k lines so extending this example in the full case is not feasible. 大约有10个.xsd文件,每个文件有10k-20k行,因此在整个情况下扩展此示例是不可行的。 Any ideas? 有任何想法吗? I would be open to other xml parsing frameworks if this functionality exits elsewhere. 如果此功能在其他地方存在,我将对其他xml解析框架持开放态度。

Note: I tried using .xjb binding with an impl class. 注意:我尝试将.xjb绑定与impl类一起使用。 It expected the impl class to be a subtype of the generated class which was the opposite relationship I was looking for. 它期望impl类是生成的类的子类型,这与我正在寻找的相反关系。

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.1"
               xmlns:xjc= "http://java.sun.com/xml/ns/jaxb/xjc"
               xmlns:xs="http://www.w3.org/2001/XMLSchema" >

    <jaxb:bindings schemaLocation="example.xsd" node="/xs:schema">
         <jaxb:bindings node="//xs:element[@name='y']">
           <jaxb:class implClass="W"/>
         </jaxb:bindings>
    </jaxb:bindings>
</jaxb:bindings>
  • Define a complex type for your y and x elements. yx元素定义一个复杂类型。
  • Write your own implementation of this type (ex. com.acme.foo.W ) 编写您自己的这种类型的实现(例如com.acme.foo.W
  • Use <jaxb:class ref="com.acme.foo.W"/> binding on this type to tell JAXB to use the W class instead of generating a new class. 在此类型上使用<jaxb:class ref="com.acme.foo.W"/>绑定,告诉JAXB使用W类而不是生成新类。

See also: 也可以看看:

JAXB: Prevent classes from being regenerated JAXB:防止重新生成类

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

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