Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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 如何在jibx中使用允许多个枚举值的属性来解组XML文档?_Java_Xml_Xsd_Unmarshalling_Jibx - Fatal编程技术网

Java 如何在jibx中使用允许多个枚举值的属性来解组XML文档?

Java 如何在jibx中使用允许多个枚举值的属性来解组XML文档?,java,xml,xsd,unmarshalling,jibx,Java,Xml,Xsd,Unmarshalling,Jibx,我想使用来解组以下XML(存储在名为test.XML的文件中): 此程序失败,错误如下: org.jibx.runtime.JiBXException:在枚举类my.target.ns.RootElement$Enumeration中找不到与值“avalue anothervalue”匹配的项 如果我像这样调整输入XML(即只设置一个枚举值),它会工作(打印AVALUE) 因此,jibx似乎不希望允许枚举值列表(我希望getAttrWithEnum返回集合,但它返回单个对象-请参见上面代码示

我想使用来解组以下XML(存储在名为test.XML的文件中):

此程序失败,错误如下:

org.jibx.runtime.JiBXException:在枚举类my.target.ns.RootElement$Enumeration中找不到与值“avalue anothervalue”匹配的项

如果我像这样调整输入XML(即只设置一个枚举值),它会工作(打印
AVALUE


因此,jibx似乎不希望允许枚举值列表(我希望
getAttrWithEnum
返回集合,但它返回单个对象-请参见上面代码示例中的注释)

当我使用(使用生成java文件)时,同样的XSD工作得很好,所以我认为我的XSD是有效的(尽管如果有更好的方法来定义我想要的东西,那就好了)

因此,我的问题是:


如何使用允许jibx中有多个枚举值的属性来解组XML文档?

我从您的模式中使用
xjc
生成了一个
RootElement
类。
attrWithEnum
属性的字段变成了
List attrWithEnum
,它将属性中的每个单词作为列表中的一个单独字符串进行分割。这将允许任何字符串值,而不仅仅是枚举中定义的字符串值

将其更改为仅
字符串attrWithEnum
当然会按原样存储属性

我将类型更改为枚举:

enum AttrEnum {
    avalue,
    anothervalue
}
@XmlAttribute(name = "attrWithEnum", required = true)
public List<AttrEnum> attrWithEnum;
enum AttrEnum{
阿瓦鲁,
另一个值
}
@XmlAttribute(name=“attrWithEnum”,必需=true)
公共列表属性和枚举;
使用JAXB(我从来没有使用过Jibx)这给了我一个只有有效值的列表。属性中未在枚举中定义的任何值都将作为
null
值返回

如果属性仅包含枚举中的一个有效值,则将字段更改为
AttrEnum attrWithEnum
只会返回非空值


因此,我猜想您的
RootElement
类将
attrWithEnum
定义为一个
enum
而不是一个枚举列表(
list

谢谢您的回答,但我将枚举放在一个列表中,因此我认为模式确实允许它-例如,请参见:You's right。这应该是可能的。你的
RootElement
类是什么样子的?我已经编辑了我的答案,并就问题可能是什么给出了一个有根据的猜测。是的,jibx将该属性定义为单个对象,而不是集合(我试图在我的问题中用括号来表示-我想我应该把它说得更清楚-我会更新这个问题)。列表会更好,因为模式验证将确保字符串在限制内有效(只要我验证模式)。因此,在这种情况下,我可以安全地从字符串转换为枚举。我也尝试了jaxb,并获得了更好的结果,但我想给jibx一个机会,因为我发现jaxb的性能很差(但它似乎更好地理解了模式)。现在我删除了我的答案,我将在这里添加一个注释:XML模式绝对正确,应该允许此属性使用枚举值列表,这是表达这个约束的最好方法。
<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="my:target:ns" xmlns="my:target:ns" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">            
    <xs:element name="rootElement">
        <xs:complexType>
            <xs:attribute name="attrWithEnum" use="required">
                <xs:simpleType>
                    <xs:list>
                        <xs:simpleType>
                            <xs:restriction base="xs:string">
                                <xs:enumeration value="avalue"/>
                                <xs:enumeration value="anothervalue"/>
                            </xs:restriction>
                        </xs:simpleType>
                    </xs:list>
                </xs:simpleType>
            </xs:attribute>
        </xs:complexType>
    </xs:element>    
</xs:schema>
package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;

import my.target.ns.RootElement;

public final class Program {

    public static void main(final String[] args) {                

        try {

            IBindingFactory bfact = BindingDirectory.getFactory(RootElement.class);
            IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
            FileInputStream in = new FileInputStream(new File("test.xml"));

            RootElement data = (RootElement) uctx.unmarshalDocument(in, null);

            // This is not what I was expecting. I was expecting 
            // List<RootElement.Enumeration> (or equivalent) not
            // a single RootElement.Enumeration instance
            RootElement.Enumeration attrValue = data.getAttrWithEnum();

            System.out.println(attrValue);

        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}
<rootElement attrWithEnum="avalue" xsi:schemaLocation="my:target:ns simple.xsd" xmlns="my:target:ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
enum AttrEnum {
    avalue,
    anothervalue
}
@XmlAttribute(name = "attrWithEnum", required = true)
public List<AttrEnum> attrWithEnum;