java - XMLBeans:获取嵌套元素的注释

标签 java xml xsd xmlbeans

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

这是 XSD。我需要访问元素 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>

我该怎么做?看来……不可能。

最佳答案

所以我在 XMLBeans FAQ 中找到了答案。这是link

我正在粘贴常见问题解答的代码,因为仅包含链接的答案会让人皱眉。您几乎可以从这个示例中找出如何使其适应您自己的需求:

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

至于属性,常见问题解答甚至没有提及它们,但它们的访问方式有所不同。这让我非常头疼,因为我试图弄清楚如何与上面的代码类似地访问属性的注释。不过,访问属性的注释非常简单明了。

这是我当前的方法:

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。

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

关于java - XMLBeans:获取嵌套元素的注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42965146/

相关文章:

javascript - 如何从 AJAX 响应更新 JSP 中的模型属性?

java - 同步器困惑

java - 初始化对象时,实例变量总是没有初始化?

java - 没有 xml 的 Hibernate

xml - JSON 作为数据库导出格式

xml - 对许多同名元素进行 XSD 属性验证 - 不可能吗?

java - 如何在RecyclerView中使用光标适配器制作多张卡片?

PHP XML 删除父级

xml - 来自 node.js 的 XSD 验证功能?

xml - 为什么 XSD 规范接受具有 -14H 的时区?