Rectangle 27 6

JAXB exports xsi:type if your data specifies other type than your model. In your case, you set a string, but the field is Object. So your data has a different type than your model. The behaviour is correct.

How you can fix that. You have align the type of the property with the type of the data. There's quite a number of ways to achieve that:

  • Make it String, why is it an Object in the first place?
@XmlElementRef
@XmlMixed

However, to keep things consistent, I cast the string to Object so that it matches the method's parameter type.

thx for confirming my understanding of what's happening. Unfortunately, I can't change the type from Object to String as the XSD has been supplied to me (I did play about the method and changed it to accept String and the xsi:type goes way like you've said). Any good links to your second suggestion or does that involve changing the JAXB classes too? I'm using Eclipselink MOXy BTW. Is there some sort of feature that I can use? When I tried to cast the String to Object I was clutching at straws but with no luck! ;)

@user3572079 You can't change the schema but you can customize, for instance using jaxb:javaType, see the update.

Thx for that. I'll have a look at your update

java - How to remove xmlns:xsi and xsi:type from JAXB marshalled XML f...

java xml eclipse jaxb moxy
Rectangle 27 3

You can always override the property type using the type parameter on the @XmlElement annotation.

@XmlElement(name = "DefaultCar", type=String.class) 
  protected Object defaultcar;

Thx Blaise, I read your solution to the other person who had a similar problem to me and thought I could use it too. However, it would be preferable not to alter the classes generated by JAXB but to code it in my program instead. I was hoping you pick up my question but it seems that EclipsLink MOXy doesn't provide any means to prevent this from happening then? :(

java - How to remove xmlns:xsi and xsi:type from JAXB marshalled XML f...

java xml eclipse jaxb moxy
Rectangle 27 2

In the article below I demonstrate how this can be achieved using NamespacePrefixMapper from the JAXB RI, and the @XmlSchema annotation with EclipseLink JAXB (MOXy):

JAXB in java 6 not prefixing the correct namespace prefix in marshalle...

namespaces jaxb prefix
Rectangle 27 1

The name of the prefix is meaningless. All it does is make a connection between a namespace and the tags that belong to that namespace. Whether the prefix is abc or namespace01 or there is no prefix because the default namespace is used doesn't matter. As far as I know you can't force the usage of a prefix or the specific name of a prefix in JAXB.

thanks musiKk, now I understand it. But if we want to use our own defined prefix, we can use JAXB RI which contains NamespacePrefixMapper along with JAXB from Java 6. [include JAXB RI in classpath]. marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespaceMapper());

@pradeep: Nice. Even though you make yourself dependent on a specific implementation it's good to know.

Don't totally agree on the "meaningless" part. It may be "meaningless" from a technical point of view. But assume that e.g. - in any way - a human will see or work with the XML, it might be important to be able to set the namespace prefixes in a meaningful way...

JAXB in java 6 not prefixing the correct namespace prefix in marshalle...

namespaces jaxb prefix
Rectangle 27 0

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

You can use the @XmlPath extension in MOXy to flatten the XML structure.

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class AccessRole {

    @XmlElement(name="accessRoleName")
    private String name;

}

Thank you so much for you solution(+1).But i am not able to flatten out the structure using your solution maybe because my AccessRole class is inherited by BaseAccessRole class having property called name which is being used by AccessRole class.

@HeenaHussain - The jaxb.properties file goes in the same package as the domain model: blog.bdoughan.com/2011/05/.

java - How to remove some elements from XML file generated using JAXB?...

java jaxb
Rectangle 27 0

I had similar problem. I was sending XML with these attributes to some WS that was not able to handle it. I remember that I was using Apache CXF for sending this XML so I ended up with CXF interceptor that handled removal of these attributes.

Sadly, I didn't find a way how to "disable" generation of these attributes directly in JAXB. What you can do (and probably will be the only way to solve it) is that you take your generated XML and process it once again with another (DOM/SAX) API and remove attributes manually. It is definitely not a nice solution but I'm not sure if you find better :-/

I would be happy if anyone gives you a better answer...

I did read some forums which mentioned writing an XML filter (I guess similar to what you've done) but I was hoping there was a more cleaner solution

java - How to remove xmlns:xsi and xsi:type from JAXB marshalled XML f...

java xml eclipse jaxb moxy
Rectangle 27 0

In the article below I demonstrate how this can be achieved using NamespacePrefixMapper from the JAXB RI, and the @XmlSchema annotation with EclipseLink JAXB (MOXy):

JAXB in java 6 not prefixing the correct namespace prefix in marshalle...

namespaces jaxb prefix
Rectangle 27 0

The name of the prefix is meaningless. All it does is make a connection between a namespace and the tags that belong to that namespace. Whether the prefix is abc or namespace01 or there is no prefix because the default namespace is used doesn't matter. As far as I know you can't force the usage of a prefix or the specific name of a prefix in JAXB.

thanks musiKk, now I understand it. But if we want to use our own defined prefix, we can use JAXB RI which contains NamespacePrefixMapper along with JAXB from Java 6. [include JAXB RI in classpath]. marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespaceMapper());

@pradeep: Nice. Even though you make yourself dependent on a specific implementation it's good to know.

Don't totally agree on the "meaningless" part. It may be "meaningless" from a technical point of view. But assume that e.g. - in any way - a human will see or work with the XML, it might be important to be able to set the namespace prefixes in a meaningful way...

Sign up for our newsletter and get our top new questions delivered to your inbox (see an example).

JAXB in java 6 not prefixing the correct namespace prefix in marshalle...

namespaces jaxb prefix
Rectangle 27 0

If you have problems using annotations, it is possible to remove them and use an instance of JAXBElement instead. To do so:

  • First delete any annotation in your Customers class public class Customers{ private ArrayList<Customer> customers; public List<Customer> getCustomers() { return customers; } public void setCustomers(ArrayList<Customer> customers) { this.customers = customers; } }
  • Second use an instance of JAXBElement in your parsing method try { File file = new File("/Users/s.xml"); JAXBContext jaxbContext = JAXBContext.newInstance(Customers.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); JAXBElement<Customers> je1 = unmarshaller.unmarshal(file, Customers.class); Customers c = je1.getValue(); System.out.println(c.getCustomers()); } catch (JAXBException e) { e.printStackTrace(); } }

However note that annotations are required when you want to override default behavior.

java - Reading an XML file with JAXB - Stack Overflow

java xml jaxb
Rectangle 27 0

JAXB exports xsi:type if your data specifies other type than your model. In your case, you set a string, but the field is Object. So your data has a different type than your model. The behaviour is correct.

How you can fix that. You have align the type of the property with the type of the data. There's quite a number of ways to achieve that:

  • Make it String, why is it an Object in the first place?
@XmlElementRef
@XmlMixed

However, to keep things consistent, I cast the string to Object so that it matches the method's parameter type.

thx for confirming my understanding of what's happening. Unfortunately, I can't change the type from Object to String as the XSD has been supplied to me (I did play about the method and changed it to accept String and the xsi:type goes way like you've said). Any good links to your second suggestion or does that involve changing the JAXB classes too? I'm using Eclipselink MOXy BTW. Is there some sort of feature that I can use? When I tried to cast the String to Object I was clutching at straws but with no luck! ;)

@user3572079 You can't change the schema but you can customize, for instance using jaxb:javaType, see the update.

Thx for that. I'll have a look at your update

java - How to remove xmlns:xsi and xsi:type from JAXB marshalled XML f...

java xml eclipse jaxb moxy
Rectangle 27 0

I had similar problem. I was sending XML with these attributes to some WS that was not able to handle it. I remember that I was using Apache CXF for sending this XML so I ended up with CXF interceptor that handled removal of these attributes.

Sadly, I didn't find a way how to "disable" generation of these attributes directly in JAXB. What you can do (and probably will be the only way to solve it) is that you take your generated XML and process it once again with another (DOM/SAX) API and remove attributes manually. It is definitely not a nice solution but I'm not sure if you find better :-/

I would be happy if anyone gives you a better answer...

I did read some forums which mentioned writing an XML filter (I guess similar to what you've done) but I was hoping there was a more cleaner solution

java - How to remove xmlns:xsi and xsi:type from JAXB marshalled XML f...

java xml eclipse jaxb moxy
Rectangle 27 0

You can always override the property type using the type parameter on the @XmlElement annotation.

@XmlElement(name = "DefaultCar", type=String.class) 
  protected Object defaultcar;

Thx Blaise, I read your solution to the other person who had a similar problem to me and thought I could use it too. However, it would be preferable not to alter the classes generated by JAXB but to code it in my program instead. I was hoping you pick up my question but it seems that EclipsLink MOXy doesn't provide any means to prevent this from happening then? :(

java - How to remove xmlns:xsi and xsi:type from JAXB marshalled XML f...

java xml eclipse jaxb moxy