1

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!

mkab
  • 863
  • 4
  • 15
  • 29
  • How does the corresponding POJO containing the `reference` element look? – Petter Mar 23 '15 at 11:15
  • @Petter: Check my edited question. – mkab Mar 23 '15 at 11:26
  • I don't understand why you get `JAXBElement`s instead of plain `String`s. Were the classes generated using the XSD? Is the `reference` element in the XSD part of a `choice` element or something similar? – Petter Mar 23 '15 at 11:37
  • 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 Mar 23 '15 at 11:42
  • Maybe this answer can help (at least with getting plain Strings): http://stackoverflow.com/questions/12508741/jaxb-generating-jaxbelementstring-instead-of-string – Petter Mar 23 '15 at 11:43
  • @Petter: Ok I'll check it out. – mkab Mar 23 '15 at 11:44
  • @Petter: I can't believe my mistake. After changing the `reference` element from `xs:unSignedShort` (its former value) to `xs:string`, I overwrote the previous JAXB classes with the generated ones. But I didn't overwrite the `ObjectFactory` class. I thought the `ObjectFactory` class is useful only if you want to create an XML file from the POJOs. It seems that JAXB uses the ObjectFactory internally though I'm not sure how. I checked the classes generated by JAXB and none of them contained an instance of this factory. I suspect it's during (un) marshalling. Well, I just lost a day debugging :( – mkab Mar 24 '15 at 10:51
  • @Petter: Do you have any idea how JAXB uses the `ObjectFactory` class? – mkab Mar 24 '15 at 10:52

1 Answers1

0

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

Cross check your POJO.

Gaurav Mishra
  • 929
  • 5
  • 11
  • Yes I printed the values out and they were without the leading zeros. I edited my question to add the POJO. – mkab Mar 23 '15 at 11:32