XMLBeans:获取嵌套元素的注释 [英] XMLBeans: Get annotation of a nested element

查看:99
本文介绍了XMLBeans:获取嵌套元素的注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取XSD中xs:complexType中声明的元素的注释.这样的元素的类型为SchemaPreperty.但是,与SchemaGlobalElement和SchemaType不同,我没有可以使用的SchemaProperty.getAnnotation().

I'm trying to get the annotation of an element that is declared within a xs:complexType in my XSD. Such an element is of type SchemaPreperty. However, unlike with SchemaGlobalElement and SchemaType, there is no SchemaProperty.getAnnotation() that I can use.

这是XSD.我需要访问元素number的文档.

This is the XSD. I need to access the documentation of element number.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="test" type="my-test-type" />

    <xs:complexType name="my-test-type">
        <xs:sequence>
            <xs:element name="number" "xs:int">
                <xs:annotation>
                    <xs:documentation>This is the documentation I need.</xs:documentation>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

我该怎么做?似乎……可能.

How do I do this? It doesn't seem... possible.

推荐答案

所以我在XMLBeans FAQ中找到了答案.这是链接

So I found the answer in the XMLBeans FAQ. Here's the link

我粘贴的是FAQ的代码,因为仅包含链接的答案就被忽略了.您可以从此示例中弄清楚如何使它适应您自己的需求:

I'm pasting the FAQ's code, since answers that just contain links are frowned upon. You can pretty much figure out how to adapt it to your own needs from this example:

public static void someMethod() {
    SchemaType t = XmlBeans.getContextTypeLoader().findType(new QName("http://test", "T"));
    SchemaProperty[] schemaProperties = t.getProperties();
    for (int i = 0; i < schemaProperties.length; i++)
        printPropertyInfo(schemaProperties[i]);

    System.out.println();

    if (t.getContentType() == SchemaType.ELEMENT_CONTENT ||
            t.getContentType() == SchemaType.MIXED_CONTENT)
    {
        SchemaParticle topParticle = t.getContentModel();
        // topParticle is non-null if we checked the content
        navigateParticle(topParticle);
    }
}

public static void navigateParticle(SchemaParticle p)
{
    switch (p.getParticleType())
    {
    case SchemaParticle.ALL:
    case SchemaParticle.CHOICE:
    case SchemaParticle.SEQUENCE:
        // These are "container" particles, so iterate over their children
        SchemaParticle[] children = p.getParticleChildren();
        for (int i = 0; i < children.length; i++)
            navigateParticle(children[i]);
        break;
    case SchemaParticle.ELEMENT:
        printElementInfo((SchemaLocalElement) p);
        break;
    default:
        // There can also be "wildcards" corresponding to <xs:any> elements in the Schema
    }
}

public static void printPropertyInfo(SchemaProperty p)
{
    System.out.println("Property name=\"" + p.getName() + "\", type=\"" + p.getType().getName()
        + "\", maxOccurs=\"" +
        (p.getMaxOccurs() != null ? p.getMaxOccurs().toString() : "unbounded") + "\"");
}

public static void printElementInfo(SchemaLocalElement e)
{
    System.out.println("Element name=\"" + e.getName() + "\", type=\"" + e.getType().getName()
        + "\", maxOccurs=\"" +
        (e.getMaxOccurs() != null ? e.getMaxOccurs().toString() : "unbounded") + "\"");
    SchemaAnnotation annotation = e.getAnnotation();
    if (annotation != null)
    {
        SchemaAnnotation.Attribute[] att = annotation.getAttributes();
        if (att != null && att.length > 0)
            System.out.println("  Annotation: " + att[0].getName() + "=\"" +
                att[0].getValue() + "\"");
    }
}

关于属性,FAQ甚至没有提到它们,但是对它们的访问方式有所不同.这让我头疼不已,因为我试图找出与上述代码相似的方法来访问属性的注释.不过,访问属性的批注非常容易和直接.

As for attributes, the FAQ doesn't even mention them, but they are accessed differently. This gave me a huge headache, because I was trying to figure out how to access an attribute's annotation similarly to the code above. Accessing an attribute's annotations is pretty easy and straightforward though.

这是我目前执行此操作的方法:

Here's my current method for doing so:

public String getAttributeAnnotation(SchemaType t, String attributeLocalName) {
    if (null != t) {
        SchemaAttributeModel attrModel = t.getAttributeModel();
        if (null != attrModel) {
            SchemaLocalAttribute[] attributes = t.getAttributeModel().getAttributes();
            if (attributes.length > 0) {
                SchemaLocalAttribute attr = Arrays.stream(attributes)
                        .filter(a -> a.getName().getLocalPart().equals(attributeLocalName))
                        .findFirst().orElse(null);
                if (null != attr) {
                    String annotationDoc = getAnnotationDocumentation(attr.getAnnotation());
                    return annotationDoc;
                }
            }
        }
    }

    return null;
}

这是我的getAnnotationDocumentation()(可以改进!).您可以使用它来检索元素和属性的xs:annotation中的xs:documentation.

Here's my getAnnotationDocumentation() (which can be improved upon!). You can use it for retrieving the xs:documentation inside an xs:annotation for both elements and attributes.

public static String getAnnotationDocumentation(SchemaAnnotation an) {
    if (null != an) {
        StringBuilder sb = new StringBuilder();
        XmlObject[] userInformation = an.getUserInformation();
        if (null != userInformation & userInformation.length > 0) {
            for (XmlObject obj : userInformation) {
                Node docInfo = obj.getDomNode();
                NodeList list = docInfo.getChildNodes();
                for (int i = 0; i < list.getLength(); i++) {
                    Node c = list.item(i);
                    if (c.getNodeType() == Node.TEXT_NODE) {
                        String str = c.getNodeValue();
                        sb.append(str.trim());
                        break;
                    }
                }
            }
        }
        return sb.toString();
    }
    return null;
}

这篇关于XMLBeans:获取嵌套元素的注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆