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

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

Related

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.

Convert XML to JSON with different property names using Jackson

I have the next task: read XML file from some directory and convert it to JSON string.
The problem: initial XML and JSON have different names for corresponding properties, e.g. x_date in XML and j_date in JSON.
I have created the class with required field for JSON with such annotations:
public class Card {
#JacksonXmlProperty(localName = "x_date")
#JsonProperty("j_date")
private String date;
// other fields
I have tried to serialize/deserialize test XML file, and it's seems work correctly.
But I'm not sure that is ok to annotate fields with #JacksonXmlProperty and #JsonProperty annotations at the same time. Maybe it's better to create one class per XML part and one for the JSON and transfer the data between them some mapper (e.g. Orika)?
Any suggestions?
Finally solved this by splitting logic in two separate classes: Card.class for XML data with help of #JacksonXmlProperty annotation and CardDto.class which uses #JsonProperty. Mapping between these classes is handled by Orika mapper.
This split will ease further customization of both classes and will allow add new functionality (e.g. persist data to the database using new entity class).

Using Jackson XML Mapper, how to serialize more than one properties using same local name

I have an instance of a class that looks as following
public class SomeEntity{
private OpMetric metric = Options.MEASURED;
private Scope scope = Scopes.GLOBAL;
}
Which need to be serialized into following XML
<SomeEntity xmlns="">
<op-metric>
<value>0.3</value>
</op-metric>
<calculated-scope>
<value>updated-global</value>
</calculated-scope>
</SomeEntity >
in both cases the value to be set in the xml is calculated based on enum values of the original fields ,meaning I need to use getters (+ #JsonIgnore on the fields ) and not just annotate the fields.
I've tried to use the following annotation on the getters to generate the format
#JacksonXmlProperty(isAttribute = false, localName = "value")
#JacksonXmlElementWrapper(localName="op-metric")
but it can only be used on one of them due to collision when using the same local name :
com.fasterxml.jackson.databind.JsonMappingException: Conflicting getter definitions for property "value":
Using Mixins did not advance me much since obviously the same limitation applies there as well.
How should I go about creating this XML structure ?
I've ended up creating special methods for the purpose of XML creation ,each of which returns an instance of a class whose only field is named "value", which is then "automatically" gets serialized into the format required .
Annotations were added in using Jackson mixin

customized property name using jaxb annotations

I have a pojo definition like following:
class Pojo<T>
{
private T entity;
//setters and getters;
}
I want to serialize this pojo into json using jackson and jaxb annotations. I want the property name in json to be the type of T not the name "entity". I tried few things with jaxb annotations but nothing worked as expected.
Any help would be much appreciated.

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.

Categories