自定义jaxb绑定

时间:2011-06-08 06:47:53

标签: jaxb

以下是使用jaxb生成的xml 任何人都可以建议一个覆盖方法来完全消除xmlns:ns3,xmlns:ns4和ns3:type

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ipsa:Address xmlns:ipsa="http://IPSA/IPSACIM/">
    <ContMed_ID xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">1</ContMed_ID>
    <ContactType xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">2</ContactType>
    <Priority xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">3</Priority>
    <InventoryLastModified xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">5</InventoryLastModified>
    <Flag xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">4</Flag>
    <Street xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">6</Street>
    <City xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">7</City>
    <State xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">8</State>
    <PostCode xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">9</PostCode>
    <Country xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">10</Country>
    <Flag xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">11</Flag>
    <InventoryLastModified xmlns:ns3="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns4="http://www.w3.org/2001/XMLSchema" ns3:type="ns4:int">12</InventoryLastModified>
</ipsa:Address>

1 个答案:

答案 0 :(得分:1)

您可以使用@XmlSchema包级别注释:

<强>包信息

@XmlSchema(xmlns={
    @XmlNs(prefix="ipsa", namespaceURI="http://IPSA/IPSACIM/"),
    @XmlNs(prefix="xsi", namespaceURI="http://www.w3.org/2001/XMLSchema-instance"),
    @XmlNs(prefix="xs", namespaceURI="http://www.w3.org/2001/XMLSchema")
})    
package your.package;


import javax.xml.bind.annotation.*;

<强>地址

package your.package;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="Address", namespace="http://IPSA/IPSACIM/")
public class Address {

    private Object contactMedID;
    private Object contactType;

    @XmlElement(name="ContMed_ID")
    public Object getContactMedID() {
        return contactMedID;
    }

    public void setContactMedID(Object contactMedID) {
        this.contactMedID = contactMedID;
    }

    @XmlElement(name="ContactType")
    public Object getContactType() {
        return contactType;
    }

    public void setContactType(Object contactType) {
        this.contactType = contactType;
    }

}

<强>演示

package your.package;

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

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Address.class);

        Address address = new Address();
        address.setContactMedID(1);
        address.setContactType(1);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(address, System.out);
    }

}

<强>输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ipsa:Address xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ipsa="http://IPSA/IPSACIM/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ContMed_ID xsi:type="xs:int">1</ContMed_ID>
    <ContactType xsi:type="xs:int">1</ContactType>
</ipsa:Address>

在package-info类上没有@XmlSchema的输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:Address xmlns:ns2="http://IPSA/IPSACIM/">
    <ContMed_ID xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">1</ContMed_ID>
    <ContactType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:int">1</ContactType>
</ns2:Address>