XMLBeans:获取嵌套元素的注释

时间:2017-03-23 00:33:46

标签: 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>

我该怎么做?它似乎不太可能。

2 个答案:

答案 0 :(得分:2)

所以我在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;
}

答案 1 :(得分:0)

我有相同的要求,并且,如果您正在寻找不同的方法,则可以在这里参考我的答案:How to read Xsd-Annotations from Xsd-Schema using Apache XMLBeans?