unexpected result using jaxb eclipselink2.2 with cdata - java

<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

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.

Jaxb: attribute fixed value in annotated class

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.

AccessOrder for Properties in JAXB

In my class, I have more than 80 attributes.
I have to do it into xml file using JAXB using the same order in class.
so please suggest me a propOrder that create automatically or some other way to give in same order as i given in class.
Note:By default i m getting output in alphabetical order
example:
Java object : order[id = 1, item = 121, qty = 10, city = QWE, ..........., addr = ASD]
excepted result : In xml file
<order>
<id>1</id>
<item no>121</item no>
<qty>10</qty>
.
.
.
.
<addr>ASD</addr>
</order>
If you are creating the xml from the Java object then use
#XmlType (propOrder={"id","item",..."addr"})
A similar post talks about is and in more details.
JAXB and property ordering
For additional check
If you are converting xml to Java object you should use sequence element if you are validating via xsd.
http://www.w3schools.com/schema/el_sequence.asp
The order that you specify the fields and properties in your class is not significant. This means that when the JAXB (JSR-222) implementation introspects the class it may not see the fields/properties in the same order that you specified them in. Alphabetical order is the easiest way to offer consistent ordering. If you want to specify an order you need to use propOrder on #XmlType.

MyBatis configuration

I want to use interface instead of mapper XML file in MyBatis. In the MyBatis configuration file I define mapper
<mapper class="aa.B" /> where B contains methods annotated with MyBatis annotations. When I try to create a mapper instance session.getMapper(B.class); I get an error:
org.xml.sax.SAXParseException: Attribute "class" must be declared for element type "mapper".
How can I use interfaces to map SQL statements here?
The exception is occurred by absence of the attribute 'class' in 'mapper' element in DTD.
For example, in MyBatis 3.0.1, 'mapper' doesn't have the 'class' attribute in DTD like this:
<!ATTLIST mapper
resource CDATA #IMPLIED
url CDATA #IMPLIED
>
You need to upgrade your MyBatis library to latest one like 3.1.1.
In the version, the attribute definition of 'mapper' is as follows:
<!ATTLIST mapper
resource CDATA #IMPLIED
url CDATA #IMPLIED
class CDATA #IMPLIED
>
You can find your DTD file in the following path in your MyBatis library:
org/apache/ibatis/builder/xml/mybatis-3-config.dtd
You can't add Mapper Interface via the sqlconfig xml file, you've to use the Java API
Configuration.addMapper(B.class)
The node in sqlconfig xml is for definding sqlmap XMLs, not Mapper Interfaces.
Or you could use mybatis-spring, which has support to auto add all the mappers in a said package, to the sqlsessionfactory's configuration.

Categories