如何在XSOM Java库中获取Use Attribute

时间:2011-11-29 09:03:48

标签: java xml xsom

我正在使用XSOM java库来解析XML Schema。 我不知道如何为属性声明获取属性“使用”。 这是获取CompleType

的所有属性声明的代码
// To get ComplexType attributes
private static void getComplexAttributes(XSComplexType xsComplexType) {
    Collection<? extends XSAttributeUse> c = xsComplexType.getAttributeUses();
    Iterator<? extends XSAttributeUse> i = c.iterator();
    while(i.hasNext()) {
        // i.next is attributeUse
        XSAttributeUse attributeUse = i.next();
        XSAttributeDecl attributeDecl = i.next().getDecl();
        System.out.println("Attributes for CoplexType: " + xsComplexType.getName());

        parseAttribute(attributeDecl, attributeUse);    
    }
}

// Get attribute info
private static void parseAttribute(XSAttributeDecl attributeDecl, XSAttributeUse attUse) { 
    System.out.println("Attribute Name: " + attributeDecl.getName());
    XSSimpleType xsAttributeType = attributeDecl.getType();
    System.out.println("Attribute Type: " + xsAttributeType.getName());
    if (attUse.isRequired())
        System.out.println("Use: Required");
    else
        System.out.println("Use: Optional");
    System.out.println("Fixed: " + attributeDecl.getFixedValue());
    System.out.println("Default: " + attributeDecl.getDefaultValue());
}

我收到此错误: java.util.NoSuchElementException

指向

XSAttributeDecl attributeDecl = i.next().getDecl();

有人可以帮忙吗?我什么都错过了?

由于

1 个答案:

答案 0 :(得分:1)

我解决了问题,谢谢

正确的代码:

// To get ComplexType attributes
private static void getComplexAttributes(XSComplexType xsComplexType) {
    Collection<? extends XSAttributeUse> c = xsComplexType.getAttributeUses();
    Iterator<? extends XSAttributeUse> i = c.iterator();
    while(i.hasNext()) {
       // i.next is attributeUse
       XSAttributeUse attUse = i.next();
       System.out.println("Attributes for CoplexType:"+ xsComplexType.getName());

       parseAttribute(attUse);      
    }
}

// To Get attribute info

private static void parseAttribute(XSAttributeUse attUse) { 
    XSAttributeDecl attributeDecl = attUse.getDecl();
    System.out.println("Attribute Name:"+attributeDecl.getName());
    XSSimpleType xsAttributeType = attributeDecl.getType();
    System.out.println("Attribute Type: " + xsAttributeType.getName());
    if (attUse.isRequired())
        System.out.println("Use: Required");
    else
        System.out.println("Use: Optional");
    System.out.println("Fixed: " + attributeDecl.getFixedValue());
    System.out.println("Default: " + attributeDecl.getDefaultValue());
}