I'm using JAXB to manipulate data from an XML file and inserting these values in to a database. I have a problem extracting a data from a particular XML tag.
This XML tag contains alphanumeric values but about 95% (if not 99%) of the time, the values it contains are integers. Therefore I treat these values as a String
.
<reference>12345</reference>
Moreover, the corresponding column in the database is a varchar
.
I created an XSD
file making sure that the the element reference
is of type xsd:string
<xs:element name="reference" type="xs:string" />
The problem is that I can have an XML file where some references are of this format:
<reference>012345</reference>
<reference>0012345</reference>
Extracting the value of these references removes the leading zeros giving me 12345
as the resulting value. I don't understand why. I feel like JAXB is treating the values as integers.
How can I go about getting the right values?
Edit:
Here's the corresponding POJO @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "referenceOrDescriptionOrEnseignes" }) public static class Programme {
@XmlElementRefs({
@XmlElementRef(name = "reference", type = JAXBElement.class, required = false),
/** other @XmlElementRef */
})
protected List<JAXBElement<?>> referenceOrDescriptionOrEnseignes;
The funny thing is that in the get method of the above variable, the JAXB specifies the objects that are allowed to the list. Only String
and other POJO objects created by JAXB using the XSD files are allowed. There is no mention of Integers
allowed.
/**
* Gets the value of the referenceOrDescriptionOrEnseignes property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the referenceOrDescriptionOrEnseignes property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getReferenceOrDescriptionOrEnseignes().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link JAXBElement }{@code <}{@link String }{@code >}
* ...
* ...
* ...
*/
public List<JAXBElement<?>> getReferenceOrDescriptionOrEnseignes() {
if (referenceOrDescriptionOrEnseignes == null) {
referenceOrDescriptionOrEnseignes = new ArrayList<JAXBElement<?>>();
}
return this.referenceOrDescriptionOrEnseignes;
}
Bizarrely, when I print the class type of the reference element, it prints Integer
.
Thanks!
reference
element look? – PetterJAXBElement
s instead of plainString
s. Were the classes generated using the XSD? Is thereference
element in the XSD part of achoice
element or something similar? – Petterreference
element isn't part of a choice. It's part of another larger object. Oh I just noticed that required attribute isfalse
which isn't right. But that's another matter. – mkab