0

I'm using JaxB to create a xml file that I need send to Government System. However, if I use xsi:type atribute in a xml tag, then the system didn't read my file and throws a error.

How can I to remove xsi:type atribute from tag ??

TestJaxB Class has the main method.

public class TestJaxB {

public static void main(String[] args) {    
    JAXBContext jaxbContext;
    try {
        jaxbContext = JAXBContext.newInstance(D.class); 
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    try{
         jaxbMarshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
            }
    catch(PropertyException pex)
    {
        System.err.println("ERRO HEADER");
        jaxbMarshaller.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
    }

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "");

    D d = new D();
    C c = new C();
    c.setAtribC("01984199000100");
    c.setAtribA("Atrib A");

    d.setA(c);

    /*B b = new B();
    b.setAtribB("01984199000100");
    b.setAtribA("Atrib A");
    d.setA(b);*/

    jaxbMarshaller.marshal(d, System.out);

    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

The Class A has two annotations @XmlSeeAlso({B.class, C.class}) @XmlTransient

CLASS A

@XmlSeeAlso({B.class, C.class})
@XmlTransient
public class A{

private String atribA;

public String getAtribA() {
    return atribA;
}

@XmlElement
public void setAtribA(String atribA) {
    this.atribA = atribA;
}
}

The Classes B and C extends A class and use @XmlType(propOrder)

CLASS B

@XmlType(propOrder = {"atribB", "atribA"})
public class B extends A{

private String atribB;

B() {       
}

B(String atrib) {
    // TODO Auto-generated constructor stub
    setAtribB(atrib);
}

public String getAtribB() {
    return atribB;
}

@XmlElement
public void setAtribB(String atribB) {
    this.atribB = atribB;
}
}

CLASS C

@XmlType(propOrder = {"atribC", "atribA"})
public class C extends  A{

private String atribC;

C() {   
}

C(String atribC) {
    setAtribC(atribC);
}

public String getAtribC() {
    return atribC;
}

@XmlElement
public void setAtribC(String atribC) {
    this.atribC = atribC;
}   
}


@XmlRootElement(name="MainTag")
public class D {

private A a;

@XmlElement
public void setA(A a) {
    this.a = a;
}

public A getA() {
    return a;
}}

RESULT with xsi atribute

  <?xml version="1.0" encoding="UTF-8" ?>
  <MainTag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="">
    <a xsi:type="c">
      <atribC>01984199000100</atribC>
      <atribA>Atrib A</atribA>
    </a>
 </MainTag>

Tnks and best regards!

nandows
  • 1
  • 2
  • What error does the "system" throw? – Alexey R. Aug 22 '17 at 14:26
  • See these questions on why it can't work like you want it to: https://stackoverflow.com/questions/11192623/remove-xsitype-xmlnsxs-and-xmlnsxsi-from-jaxb-generics and https://stackoverflow.com/questions/27129969/how-to-remove-xmlnsxsi-and-xsitype-from-jaxb-marshalled-xml-file – tima Aug 22 '17 at 14:42
  • A Internal error. This error doen't happen if I delete xsi:type atribute from xml tag. – nandows Aug 22 '17 at 18:37

0 Answers0