开发者

customizing jaxb bindings

开发者 https://www.devze.com 2023-03-11 15:01 出处:网络
The below is the xml generated using jaxb can anyone please suggest an override method to completely eliminate xmlns:ns3,xmlns:ns4 and ns3:type

The below is the xml generated using jaxb can anyone please suggest an override method to completely eliminate xmlns:ns3,xmlns:ns4 and 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/XMLS开发者_开发知识库chema" 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>


You can use the @XmlSchema package level annotation:

package-info

@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.*;

Address

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

}

Demo

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

}

Output

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

Output without @XmlSchema on package-info class

<?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>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号