java-使用XSOM从元素获取minOccurs属性
作者:互联网
如何使用XSOM解析器从元素中获取minOccurs属性?我已经看到了获取与复杂类型相关的属性的示例:
private void getAttributes(XSComplexType xsComplexType){
Collection<? extends XSAttributeUse> c = xsComplexType.getAttributeUses();
Iterator<? extends XSAttributeUse> i = c.iterator();while(i.hasNext()){
XSAttributeDecl attributeDecl = i.next().getDecl();
System.out.println("type: "+attributeDecl.getType());
System.out.println("name:"+attributeDecl.getName());
}
}
但是,似乎无法找出正确的方法将其从诸如以下元素中删除:
<xs:element name="StartDate" type="CommonDateType" minOccurs="0"/>
谢谢!
解决方法:
因此,这并不是那么直观,但是XSElementDecl来自XSParticles.我可以使用以下代码检索相应的属性:
public boolean isOptional(final String elementName) {
for (final Entry<String, XSComplexType> entry : getComplexTypes().entrySet()) {
final XSContentType content = entry.getValue().getContentType();
final XSParticle particle = content.asParticle();
if (null != particle) {
final XSTerm term = particle.getTerm();
if (term.isModelGroup()) {
final XSParticle[] particles = term.asModelGroup().getChildren();
for (final XSParticle p : particles) {
final XSTerm pterm = p.getTerm();
if (pterm.isElementDecl()) {
final XSElementDecl e = pterm.asElementDecl();
if (0 == e.getName().compareToIgnoreCase(elementName)) {
return p.getMinOccurs() == 0;
}
}
}
}
}
}
return true;
}
标签:xsd,java,xsom 来源: https://codeday.me/bug/20191210/2100913.html