XML element with attribute has ":" in JAXB - java

I have a problem when I am getting the attributes of a xml file. These attributes are written like this:
#XmlAttribute(name="xml:lang")
With #XmlAttribute I get attributes that they are written on a one word. If I leave this annotation #XmlAttribute(name="xml:lang") I get null
How can I get attribute's value of "xml:lang"?
Thanks.

xml is clearly a namespace prefix. You can try stating the namespace it with:
#XmlAttribute(name = "lang",
namespace = javax.xml.XMLConstants.XML_NS_URI)

Related

JaxB how can i have custom attribute name in #XmlElement annotation

I am trying to define XML from Java object binding using JAXB. Everything works fine except i want to have currency tag with xml tag like this
<money currency="INR">1230</money>
I am not able to find the solution for this how can i accomadate **currency="INR" ** in the same tag
#XmlElement(name="money")
private Integer money;
Saw that #xmlElement doesnt has any properties for the same

Getting the xmlns value with Jackson XML?

I'm currently trying to get xmlns value. I need to validate xml files. I'm using Jackson-dataformat-xml to deserialize xml files to objects. So far I've not found a way to get that value.
#JacksonXmlProperty(isAttribute = true) does not seem to be working for xmlns.
xml
<Document xmlns="urn:...">
...
</Document>
Java
#Data
public class Document {
#JacksonXmlProperty(isAttribute = true)
private String xmlns;
}
As I understand xmlns isn't exposed as a attribute. Is there any I could get the value somehow with Jackson?
xmlns value is namespace declarations. it's not a attributes but metadata, so parsers do not expose them as attributes.

Which attribute in XSD will annotate a field with #XmlElement(required = false) in JAXB generated classes

I want to annotate a field in my jaxb generated class with this annotation - #XmlElement(required = false). Which attribute in the XSD would generate my field with this annotation?.
I can't hand type this as the JAXB classes are auto generated using Maven every time a build is run.
My jaxb version is xjc 2.2.4-2
Thanks
When an element has minOccurs="0" the corresponding #XmlElement has required=false. Note that false is the default value of the required attribute so it may not actually appear in the generated annotation.
UPDATE
Based on your comment:
Let me explain my actual problem. I'm using Jackson to generate the
JSON from the JAXB classes. Issue is when the element is not
present in the xml, I see the json output with the field name as 'pip'
and value as null. I am actually expecting the field 'pip' to be
absent from my json output as I declared it to be minOccurs=0 in the
XSD. Can't figure out if it's an issue with JAXB or Jackson.
Interestingly when I annotate the field explicitly with required=false
in the jaxb class, I see my expected output with the field being
absent
This is an issue with Jackson not handling the default value of the required property on the #XmlElement annotation correctly.

Remove xsi namespace in element

I'm using CXF to generate java classes from a WSDL/XSD and later back to XML (for JMS).
In one of the generated classes, it says:
#XmlElement(namespace = "http://www.example.com/", nillable = true)
protected Datum datum;
All good, but when I put a null value:
test.setDatum(null);
I get the following XML generated:
<datum xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
The xsi:nil="true" is what i want, but I don't want the namespace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance", because this namespace is already specified at the root element. Also the validation fails at the test platform we have to use.
How do I get rid of the extra namespace in the element?
I did it the other way around. I wrote a interceptor that checks for certain attributes. If found, I add the xsi:nil="true" attribute to the xml.

Tell JAXB to unmarshal <xs:dateTime> to Date class using annotations

When using JAXB with Java-First, fields/properties of type java.util.Date are marshalled and unmarshalled as xs:dateTime and everything works as expected.
But if the type of the field/property is Object, JAXB unmarshals xs:dateTimeto XMLGregorianCalendarImpl.
I need to find a way that makes JAXB unmarshal date-time values to java.util.Date by itself using annotations. Otherwise, I'll need to go through all unmarshalled values in each use case.
Even if there were some after-unmarshall-hook to define on the classes containing Object fields and convert the instances manually would be good. But I couldn't find anything that can be used this way either.
Note that I have limited access to the JAXB context, as it is used inside Apache CXF.
In addition to Blaise Doughan's answer:
I could finally figure this out, thanks for help from Blaise Doughan. Actually his answer works with just a small change: if there's several types expected to be unmarshalled as the Object property, there needs to be multiple #XmlElement annotations placed on it using #XmlElements annotation.
Here's my code now:
#XmlElements
({
#XmlElement(name = "dateValue", type = Date.class),
#XmlElement(name = "stringValue", type = String.class),
#XmlElement(name = "booleanValue", type = Boolean.class),
#XmlElement(name = "listValue", type = ArrayList.class),
#XmlElement(name = "bytesValue", type = Byte[].class)
})
public Object getFieldValue()
{
return fieldValue;
}
Note: specifying "name" is required for this to work, since there should be a way for the marshaller / unmarshaller to identify the type of the content.
There are two minor issues here:
You need to specify a list of all of the types expected (which is logical, given the case of marshalling)
There's no way to specify a single name for this property. In my case, where JAXB is used in CXF web services, code generated from WSDL in .NET names this field as "Item". If there was a way, for example, to wrap the XML elements in another one which has a single name, the generated code could be a little bit nicer.
You can set the type property on #XmlElement.
#XmlElement(type=Date.class)
public Object getGenericDateProperty() {
return date;
}
Edit:
Since you don't know the type you could use an XmlAdapter. If the unmarshalled value is an XMLGregorianCalendar convert it to a Date. For more info on XmlAdapter see:
http://bdoughan.blogspot.com/2010/07/xmladapter-jaxbs-secret-weapon.html

Categories