简体   繁体   English

使用 JAXB 将两个同构 XML 模式解析为一个类结构

[英]Parsing two isomorphous XML schemas into one class structure using JAXB

Consider two isomorphous XML schemas.考虑两个同构的 XML 模式。 By isomorphism here I mean that these two schemas have identical structures except attributes and tags names.这里的同构是指除了属性和标签名称之外,这两个模式具有相同的结构。 More specifically I have live example when was schema, say A , and its copy B , where all tags and attribute names were translated from English into national lamguage equivalents.更具体地说,我有一个活生生的例子,比如说A和它的副本B ,其中所有标签和属性名称都从英语翻译成国家语言等价物。

For example, as input we can have two different variants of one object:例如,作为输入,我们可以有一个对象的两种不同变体:

<tag_1_v1>
    <tag_2_v1 id="blabla" name="xxxxx">
        Some value1
    </tag_2_v1>
    <tag_3_v1 id="alalala" name="yyyyy">
        Some value2
    </tag_3_v1>
</tag_1_v1>

and

<tag_1_v2>
    <tag_2_v2 special_id_2="blabla" name="xxxxx">
        Some value1
    </tag_2_v2>
    <tag_3_v2 id="alalala" special_name_2="yyyyy">
        Some value2
    </tag_3_v2>
</tag_1_v2>

The problem is to map these two schemas on single class structure, say问题是将这两个模式映射到单个类结构上,例如

class Tag1 {
    Tag2 tag2;
    Tag3 tag3;
}

class Tag2 {
    String id;
    String name;
    String value;
}  

class Tag3 {
    String id;
    String name;
    String value;
} 

There are various ideas how to workaround this issue, but all of them aren't so convinient, as any possibility to use single JAXB annotation scheme on same class structure.如何解决这个问题有各种想法,但它们都不是那么方便,因为有可能在同一类结构上使用单个 JAXB 注释方案。 They are:他们是:

  1. create two different class-sets and then copy values from objects of one schema into another;创建两个不同的类集,然后将值从一个模式的对象复制到另一个模式中;
  2. create own SAX parser implementation and "translate" inside it tag and attribute names into appropriate ones;创建自己的 SAX 解析器实现并将其中的标签和属性名称“翻译”为适当的名称;
  3. use own preprocessor of XML and use string replacement (will not work if id and attributes name aren't identical within all schema).使用自己的 XML 预处理器并使用字符串替换(如果 id 和属性名称在所有模式中都不相同,则不起作用)。

Since each <tag_i> can have different attributes, a clean solution would be to use inheritance:由于每个<tag_i>可以有不同的属性,一个干净的解决方案是使用继承:

  • Create an abstract class Tag1 that is inherited by Tag1V1 and Tag1V2 .创建一个由Tag1V1Tag1V2继承的抽象类Tag1 Factor all the common code into Tag1 .将所有公共代码分解为Tag1
  • The same would go Tag2 and Tag3 . Tag2Tag3

To get you started, here would be an implementation of Tag2 :为了让您开始,这里是Tag2一个实现:

@XmlRootElement
@XmlSeeAlso({Tag2V1.class, Tag2V2.class})
abstract class Tag2 {

    private String name;
    private String content;

    @XmlAttribute(name = "name")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @XmlValue
    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

}

@XmlRootElement(name = "tag_2_v1")
class Tag2V1 extends Tag2 {

    private String id;

    @XmlAttribute(name = "id")
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

@XmlRootElement(name = "tag_2_v2")
class Tag2V2 extends Tag2 {

    private String specialId2;

    @XmlAttribute(name = "special_id_2")
    public String getSpecialId2() {
        return specialId2;
    }

    public void setSpecialId2(String specialId2) {
        this.specialId2 = specialId2;
    }

}

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

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