настройка привязок jaxb

#jaxb

#jaxb

Вопрос:

Ниже приведен xml, сгенерированный с использованием jaxb, может кто-нибудь, пожалуйста, предложить метод переопределения, чтобы полностью исключить 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. hwellmann.blogspot.com/2011/03/…

Ответ №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>
  

Вывод без @XMLSchema в классе package-info

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