XMLBeans: Get annotation of a nested element


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.

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.


So I found the answer in the XMLBeans FAQ. Here's the <a href="https://wiki.apache.org/xmlbeans/XmlBeansFaq#schemaPropertySchemaParticle" rel="nofollow">link</a>

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

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

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


I had the same requirement and , if you are looking for a different approach, you may refer here at my answer :<a href="https://stackoverflow.com/questions/48196744/how-to-read-xsd-annotations-from-xsd-schema-using-apache-xmlbeans?answertab=active#tab-top" rel="nofollow">How to read Xsd-Annotations from Xsd-Schema using Apache XMLBeans?</a>


  • Uploading a file in Java Play 1.2.3 then storing file as byte array causes PersistenceException
  • How to parse HTML in C++? [closed]
  • KSS Styleguide with Angular 4
  • How do I go about Flushing STDIN here?
  • Reading from a text file and storing in a 2D array [duplicate]
  • How to put text from file into array
  • How can I convert little endian to big endian using htonl
  • perl, read blocking using IO::Select and IO::Socket::INET
  • program for LC3 Assembly language
  • implementing a spellcheck algorithm [duplicate]
  • Upcasting and Overloading Stream Operator
  • Why is my serial communication not working?
  • reordering XML tags
  • Read and Write within a file in C (double it)
  • Transact-SQL - how to prevent second batch from running if the first failed?
  • Open createObjectURL Blob on Cordova InAppBrowser
  • R: Knitr gives error for SQL-chunk
  • How to use SpEL to inject result of method call in Spring?
  • Base64 encode binary uploaded data on the AppEngine
  • Smack 4.1.0 android Roster not displaying
  • If statement skipping right to else after being called once?
  • How to copy styled text in JTextPane
  • Reading a file into a multidimensional array
  • App restarts from wrong activity
  • Jetty Server not starting: Unable to establish loopback connection
  • How to install a .deb file on a jailbroken iphone programmatically?
  • How to delay loading a property with linq to sql external mapping?
  • Recording logins for password protected directories
  • SignalR .NET Client Invoke throws an exception
  • Lost migrations and Azure database is now out of sync
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • How do I fake an specific browser client when using Java's Net library?
  • Sony Xperia Z Tablet not found by adb
  • How to get a value (ex: baseURL) in every Karate feature?
  • Validaiting emails with Net.Mail MailAddress
  • sending/ receiving email in Java
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • How does Linux kernel interrupt the application?