How can I decouple an XML document with an attribute that allows multiple enumeration values ​​in jibx?

I want to use Jibx to unmount the following XML (stored in a file called test.xml):

<?xml version="1.0" encoding="UTF-8"?>
<rootElement attrWithEnum="avalue anothervalue" xsi:schemaLocation="my:target:ns simple.xsd" xmlns="my:target:ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</rootElement>

I defined the schema (in a file called simple.xsd) as follows:

<?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>

generated Java files from it using a tool org.jibx.schema.codegen.CodeGenand wrote this test program:

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

This program crashes with an error:

org.jibx.runtime.JiBXException: No matches were found for the value 'avalue anothervalue' in the enumerated class my.target.ns.RootElement $ Enumeration

If I configure my input XML this way (that is, I only set one enumeration value), it works (outputs AVALUE).

<rootElement attrWithEnum="avalue" xsi:schemaLocation="my:target:ns simple.xsd" xmlns="my:target:ns" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

, , jibx , ( , getAttrWithEnum , - . ).

XSD , jaxb ( java xjc), , XSD (, , , ).

:

XML- , jibx?

+4
1

RootElement xjc . attrWithEnum List<String> attrWithEnum, . , , .

String attrWithEnum, , .

:

enum AttrEnum {
    avalue,
    anothervalue
}
@XmlAttribute(name = "attrWithEnum", required = true)
public List<AttrEnum> attrWithEnum;

JAXB ( Jibx), . , , null.

AttrEnum attrWithEnum , .

, RootElement attrWithEnum enum, enum (List<AttrEnum>)

+1

Source: https://habr.com/ru/post/1584423/


All Articles