更改生成的 XJC 的方法名称 class

Changing the method name of a XJC generated class

我正在尝试编写一个允许提供外部系统状态的监控系统...通过 ping 服务器、查看日志元素、查询数据库、访问 Web 服务等。由于每个应用程序都有独特的行为,监控系统需要灵活,以允许监视器最适合这些行为。

所以这是 XSD 的一部分,用于创建 "Test" class 允许用户构建监视器:

<xs:element name="Test">
    <xs:complexType>
        <xs:sequence maxOccurs="unbounded" >
            <xs:element ref="Ping"/>
            <xs:element ref="CheckWebService"/>
            <xs:element ref="CheckDB"/>
            <xs:element ref="ExecuteScript"/>
            <xs:element ref="CheckJMS"/>
            <xs:element ref="CheckLog" />
        </xs:sequence>
        <xs:attribute name="testTitle"/>
    </xs:complexType>
</xs:element>

运行 通过 XJC(通过 Maven JAXB 插件)产生:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "pingsAndCheckLogsAndCheckJMs"
})
@XmlRootElement(name = "Test")
public class Test
    implements Serializable
{

    private final static long serialVersionUID = 1L;
    @XmlElements({
        @XmlElement(name = "Ping", required = true, type = Ping.class),
        @XmlElement(name = "CheckLog", required = true, type = CheckLog.class),
        @XmlElement(name = "CheckJMS", required = true, type = CheckJMS.class),
        @XmlElement(name = "ExecuteScript", required = true, type = ExecuteScript.class),
        @XmlElement(name = "CheckDB", required = true, type = CheckDB.class),
        @XmlElement(name = "CheckWebService", required = true, type = CheckWebService.class)
    })
    protected List<Serializable> pingsAndCheckLogsAndCheckJMs;
    @XmlAttribute(name = "testTitle")
    @XmlSchemaType(name = "anySimpleType")
    protected String testTitle;

    public List<Serializable> getPingsAndCheckLogsAndCheckJMs() {
        if (pingsAndCheckLogsAndCheckJMs == null) {
            pingsAndCheckLogsAndCheckJMs = new ArrayList<Serializable>();
        }
        return this.pingsAndCheckLogsAndCheckJMs;
    }

    public String getTestTitle() {
        return testTitle;
    }

    public void setTestTitle(String value) {
        this.testTitle = value;
    }
}

我的问题是,我如何重命名 pingsAndCheckLogsAndCheckJMs 方法,因为每次我添加新的测试类型 (Ping/CherckDB/CheckLog...) 此方法名称都会更改,以及我编组对象时的 XML 标记也很丑陋。

您需要指定一个绑定来显式命名 属性。

这里是如何内联的例子。有关详细信息,请参阅 Customizing JAXB Bindings

总结:您需要向 <xs:schema> 元素添加 2 个属性:

<xs:schema ...
           xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
           jxb:version="1.0">

<xs:sequence> 元素内的以下内容,当然将 属性 命名为您认为合适的名称:

<xs:sequence maxOccurs="unbounded">
    <xs:annotation>
        <xs:appinfo>
            <jxb:property name="foo"/>
        </xs:appinfo>
    </xs:annotation>
    ...

以下完整 Minimal, Complete, and Verifiable example (MCVE):

XSD 文件

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema version="1.0" targetNamespace="http://example.com/test"
           xmlns="http://example.com/test"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
           jxb:version="1.0">
    <xs:element name="Test">
        <xs:complexType>
            <xs:sequence maxOccurs="unbounded">
                <xs:annotation>
                    <xs:appinfo>
                        <jxb:property name="foo"/>
                    </xs:appinfo>
                </xs:annotation>
                <xs:element ref="Ping"/>
                <xs:element ref="CheckLog"/>
            </xs:sequence>
            <xs:attribute name="testTitle"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="Ping">
        <xs:complexType>
            <xs:sequence>
            </xs:sequence>
            <xs:attribute name="action" type="xs:string"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="CheckLog">
        <xs:complexType>
            <xs:sequence>
            </xs:sequence>
            <xs:attribute name="action" type="xs:string"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

测试

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import com.example.test.CheckLog;
import com.example.test.Ping;
import com.example.test.Test;

public class Test8 {
    public static void main(String[] args) throws Exception {
        Test root = new Test();
        root.setTestTitle("My title");
        root.getFoo().add(newPing("ping 1"));
        root.getFoo().add(newCheckLog("check log 1"));
        root.getFoo().add(newPing("ping 2"));

        JAXBContext jaxbContext = JAXBContext.newInstance(Test.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(root, System.out);
    }
    private static Ping newPing(String action) {
        Ping obj = new Ping();
        obj.setAction(action);
        return obj;
    }
    private static CheckLog newCheckLog(String action) {
        CheckLog obj = new CheckLog();
        obj.setAction(action);
        return obj;
    }
}

输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Test xmlns="http://example.com/test" testTitle="My title">
    <Ping action="ping 1"/>
    <CheckLog action="check log 1"/>
    <Ping action="ping 2"/>
</Test>

如你所见,方法现在命名为getFoo(),生成的XML.

也没有丑陋的地方

以上是使用jdk1.8完成的。0_151.