Jaxb: attribute fixed value in annotated class - java

I generate XML schema (XSD) from Java classes with JAXB. I wonder how to specify the value of a static attribute by using annotations.
For example I define an attribute like this
#XmlAttribute(name="tooltip")
private static final String TOOLTIP = "A string";
And I want to get in my XSD
<attribute name="tooltip" type="string" fixed="A string">
So, how can I force the generation of static attributes in XSD with JAXB?
Thanks !

As of JAXB 2.2, there isn't standard JAXB (JSR-222) metadata that can add to your model to cause the fixed attribute to appear in the generated XML Schema. The schema generation errs on the side of being too permissive rather than too restrictive. This means you can't do the following:
Mark a fixed value for an attribute
Mark a maxOccurs other than 1 or unbounded
etc.

Related

How to validate the name property of an element in XML file using javax xml validation

I have the following XML file:
<node name="FIXED_NAME">
...
</node>
Note that all elements in my XML are node (same name) and I can not change them for some reasons.
I am using java xml validator and xsd file for validation.
I want to validate that the name property for the root node must be FIXED_NAME.
How my xsd file should be?
I cannot think of any solution to your problem, for these reasons:
XSD 1.0 identifies the type of a tag by matching the hierarchy of tags in the XML document against the element/type definitions in the XSD. In your case, there is no hierarchy of names (because all tag names are the same).
XSD 1.1 provides some extra flexibility via xs:alternative and xs:assert - but they do not allow references to the parent axis.
For these reasons, I cannot think of any way to use the features of XML Schema to describe your XML.

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.

unexpected result using jaxb eclipselink2.2 with cdata

<ITEM>
<PRODUCT_NAME>IT'S ALL ABOUT YOU-LG:9"H RUBY GATHERNG VASE,RD ROSE,LIME GRN CARN,PURP STOCK,LAV POMS,ATHOS POM,SEAFOAM STATICE,SALAL</PRODUCT_NAME>
<PRODUCT_CODE>90949L</PRODUCT_CODE>
<PRODUCT_TYPE>FPT</PRODUCT_TYPE>
<PRODUCT_CAT>Floral</PRODUCT_CAT>
<ALIAS_NAME>IT'S ALL ABOUT YOU(TM) - LARGE</ALIAS_NAME>
<DELIVERY_DATE>10/11/2012</DELIVERY_DATE>
<FLEX_DATE></FLEX_DATE>
<FLEX_TEXT></FLEX_TEXT>
<QUANTITY>1</QUANTITY>
<PRICE>69.99</PRICE>
<CARD_MESSAGE>IT'S ALL ABOUT YOU...
when i dont add #XmlCDATA annotation i am getting only one product name element. But when i use cdata annotation i am getting duplicate.One with cdata and without cdata
By default a JAXB (JSR-222) implementation will expect annotations to be on the get (or set) methods. If you annotate a field this can cause a property to appear multiple times in the XML representation.
For More Information
http://blog.bdoughan.com/search/label/XmlAccessorType
http://blog.bdoughan.com/2010/07/cdata-cdata-run-run-data-run.html

how to get warned when there is a name collision in jaxb

I have the following xml:
<parameters>
<parameter >value1</param>
<parameter >value2</param>
</parameters>
with the following class:
#XmlRootElement
public class Parameters {
#XmlElement public String parameter;
}
How can I get warned (exception?) of the duplicate values when unmarshaling the xml to object?
You should have a schema a make a schema validation. If your schema permits duplicates then maybe you should switch from String to a Collection type.
If you don't have a schema, you could use schemagen tool to generate it from your java classes.
You could even generate a schema at runtime, maybe cache it together with the JaxbContext and then use it for validation.

Question on XML parsing

When I am parsing an xml string as below I get strange attributes like "autowire" with value "default". Is there anyway I can get only the attributes that are explicitly defined?
<bean id="aaaa" class="com.test.Service">
<property name="cccc" ref="cccc"/>
</bean>
I am doing simple parsing turning it into a Document and then iterating over the Nodes.
Document document = docBuilder.parse(input);
NodeList nodeList = document.getChildNodes();
etc.
You can use following APIs to find whether an attribute is explicitly specified or not:
if you are using DOM:
Attr.getSpecified()
if you are using SAX:
Attributes2.isSpecified(qname)
It depends what you are using to parse. I'm guessing this is a Spring bean configuration file. Usually there's a XML Schema associated with it and that will dictate all default values for attributes.
So when the actual XML parser walks through the document, it will build some kind of representation (DOM parsers will obviously build a tree, SAX parsers will fire off events, etc) of the XML and insert those default values.

Categories