Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JAXB从XML文件中提取值_Java_Xml_Xsd_Jaxb - Fatal编程技术网

Java 使用JAXB从XML文件中提取值

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<

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

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