Java 使用JAXB从XML文件中提取值
我使用JAXB操作XML文件中的数据,并将这些值插入到数据库中。我从特定的XML标记中提取数据时遇到问题。 这个XML标记包含字母数字值,但大约95%(如果不是99%)的时间,它包含的值是整数。因此,我将这些值视为Java 使用JAXB从XML文件中提取值,java,xml,xsd,jaxb,Java,Xml,Xsd,Jaxb,我使用JAXB操作XML文件中的数据,并将这些值插入到数据库中。我从特定的XML标记中提取数据时遇到问题。 这个XML标记包含字母数字值,但大约95%(如果不是99%)的时间,它包含的值是整数。因此,我将这些值视为字符串 <reference>12345</reference> 问题是我可以有一个XML文件,其中一些引用的格式如下: <reference>012345</reference> <reference>0012345<
字符串
<reference>12345</reference>
问题是我可以有一个XML文件,其中一些引用的格式如下:
<reference>012345</reference>
<reference>0012345</reference>
有趣的是,在上述变量的get方法中,JAXB指定了允许进入列表的对象。只允许使用XSD文件创建String
和其他由JAXB创建的POJO对象。不允许提及整数
/**
* 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;
}
*
*
*
*列表中允许以下类型的对象
*{@link JAXBElement}{@code}
* ...
* ...
* ...
*/
公共列表>();
}
返回此.ReferenceOrderDescriptionRenseignes;
}
奇怪的是,当我打印引用元素的类类型时,它会打印Integer
谢谢 在插入数据库之前,是否可以尝试打印此值
交叉检查您的POJO。包含
引用
元素的对应POJO看起来如何?@peter:检查我编辑的问题。我不明白为什么您得到的是JAXBElement
s而不是普通的字符串
s。这些类是使用XSD生成的吗?XSD中的reference
元素是choice
元素还是类似的元素?是的,我也不明白。是的,这些类是使用XSD生成的。reference
元素不是选项的一部分。它是另一个更大物体的一部分。哦,我刚刚注意到必需的属性是false
,这是不对的。但这是另一件事。也许这个答案能有所帮助(至少在获取纯字符串时):是的,我打印了这些值,它们没有前导零。我编辑了我的问题以添加POJO。
@XmlElementRefs({
@XmlElementRef(name = "reference", type = JAXBElement.class, required = false),
/** other @XmlElementRef */
})
protected List<JAXBElement<?>> referenceOrDescriptionOrEnseignes;
/**
* 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;
}