그래서 XMLBeans FAQ에서 대답을 찾았습니다. 여기에 있습니다. link
링크가 포함 된 답변에 싫은 내색이 들어가기 때문에 FAQ 코드를 붙여 넣습니다.
이
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;
}
스키마를 해석하기위한 XMLBeans의 설명서가 실제로 부족합니다. 이 코드는 작동하여 형식 정의의 자식으로 선언 된 주석을 읽습니다. 불행히도 요소에서 직접 선언 된 주석을 읽지 않습니다 (형식 정의를 사용하는지 여부). – Gio