首页 文章

使用JAXB从XML文件中提取值

提问于
浏览
1

我正在使用JAXB来处理XML文件中的数据并将这些值插入到数据库中 . 我在从特定XML标记中提取数据时遇到问题 . 此XML标记包含字母数字值,但大约95%(如果不是99%)的时间,它包含的值是整数 . 因此,我将这些值视为 String .

<reference>12345</reference>

而且,数据库中的相应列是 varchar . 我创建了一个 XSD 文件,确保元素 reference 的类型为 xsd:string

<xs:element name="reference" type="xs:string" />

问题是我可以有一个XML文件,其中一些引用具有以下格式:

<reference>012345</reference>
<reference>0012345</reference>

提取这些引用的值会删除前导零,从而将 12345 作为结果值 . 我不明白为什么 . 我觉得JAXB将值视为整数 .

我怎样才能获得正确的 Value 观?

Edit

这是相应的POJO @XmlAccessorType(XmlAccessType.FIELD)@XmlType(name =“”,propOrder = {“referenceOrDescriptionOrEnseignes”})public static class Program {

@XmlElementRefs({

    @XmlElementRef(name = "reference", type = JAXBElement.class, required = false),
    /** other @XmlElementRef  */
})
protected List<JAXBElement<?>> referenceOrDescriptionOrEnseignes;

有趣的是,在上面变量的get方法中,JAXB指定了允许列表的对象 . 仅允许使用XSD文件由JAXB创建的 String 和其他POJO对象 . 没有提到 Integers 允许 .

/**
 * 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;
}

奇怪的是,当我打印引用元素的类类型时,它会打印 Integer .

谢谢!

1 回答

  • 0

    您可以尝试在插入数据库之前打印此值 .

    交叉检查您的POJO .

相关问题