有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java更改XJC生成的类的方法名

我试图写一个监控系统,允许提供外部系统状态的状态。。。通过ping服务器、查看日志元素、查询数据库、访问web服务等。由于每个应用程序都有独特的行为,因此监控系统需要灵活,以使监控器能够最佳地适应这些行为

下面是XSD的一部分,用于创建“Test”类,该类允许用户构建监视器:

<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;
    }
}

我的问题是,既然每次我添加一个新的测试类型(Ping/CherckDB/CheckLog…),我如何重命名pingsAndCheckLogsAndCheckJMs方法此方法名称会更改,封送对象时XML标记也会更改


共 (1) 个答案

  1. # 1 楼答案

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

    下面是如何内联完成的示例。有关更多信息,请参见Customizing JAXB Bindings

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

    <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.