1
votes

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!

1
How does the corresponding POJO containing the reference element look?Petter
@Petter: Check my edited question.mkab
I don't understand why you get JAXBElements instead of plain Strings. Were the classes generated using the XSD? Is the reference element in the XSD part of a choice element or something similar?Petter
Yes I don't get that either. Yes the classes were generated using the XSD. The reference element isn't part of a choice. It's part of another larger object. Oh I just noticed that required attribute is false which isn't right. But that's another matter.mkab
Maybe this answer can help (at least with getting plain Strings): stackoverflow.com/questions/12508741/…Petter

1 Answers

0
votes

Could you try to print this value before inserting into database.

Cross check your POJO.