简体   繁体   English

如何使用 MOXy Dynamic JAXB 查找是否需要 XSD 元素?

[英]How to find if an XSD element is required using MOXy Dynamic JAXB?

I'm trying to get information out of an XSD using MOXy's DynamicJAXBContext.我正在尝试使用 MOXy 的 DynamicJAXBContext 从 XSD 中获取信息。 One of the properties I'm trying to get is if an attribute or element is required .我试图获取的属性之一是是否需要属性或元素。 From what I understand, if an element has minOccurs="1" it means it is required .据我了解,如果一个元素具有minOccurs="1" ,则表示它是required

The issue is that I haven't found a way to get this.问题是我还没有找到解决方法。

Here's the code I have until now:这是我到目前为止的代码:

DynamicJAXBContext jaxbContext = 
                DynamicJAXBContextFactory.createContextFromXSD(is, new MyEntityResolver(), null, null);


        Collection<ClassDescriptor> descriptors = jaxbContext.getXMLContext().getSession().getDescriptors().values();
        for (ClassDescriptor desc : descriptors) {
            if (desc.getJavaClassName() != null) {
                System.out.println("=================================");
                System.out.println("Class: " + desc.getJavaClassName());
                desc.getMappings().forEach(dm ->{
                    System.out.println(dm.getAttributeName());

                    System.out.println(dm.getClass().getName());
                    if(dm instanceof XMLDirectMapping) {
                        XMLDirectMapping xmlInfo = (XMLDirectMapping)dm;
                        System.out.println(xmlInfo);
                    }
                    if(dm.getAttributeClassification() != null) {
                        System.out.println(dm.getAttributeClassification().getName());
                    }
                    if(dm.getReferenceDescriptor() != null) {
                        System.out.println(dm.getReferenceDescriptor().getJavaClassName());
                    }
                 });

            }
        }

Until now, I have been able to get information regarding the attribute type, if it's a collection and the collection type.到目前为止,我已经能够获取有关属性类型的信息,如果它是一个集合和集合类型。

I've tried exploring other methods of the dm variable including isOptional() and getField().isNullable() and both return true for attributes where minOccurs="1"我尝试探索dm变量的其他方法,包括isOptional()getField().isNullable()并且对于minOccurs="1"的属性都返回true

I found out that the object returned by the getField() is actually of type XMLField which is a subclass of the general type returned by that method DatabaseField .我发现getField()返回的 object 实际上是XMLField类型,它是该方法DatabaseField返回的一般类型的子类。 The XMLField has an isRequired() method that effectively returns if the attribute is required. XMLField有一个isRequired()方法,如果需要该属性,则该方法有效地返回。

 if(dm.getField() instanceof XMLField) {
     XMLField field = (XMLField)dm.getField();
     System.out.println(field.isRequired());
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM