【问题标题】:How to get Use Attribute in XSOM Java library如何在 XSOM Java 库中获取使用属性
【发布时间】:2015-02-11 06:50:36
【问题描述】:

我正在使用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();

有人可以帮忙吗?我有什么想念的吗?

谢谢

【问题讨论】:

    标签: java xml xsom


    【解决方案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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2015-06-13
      • 1970-01-01
      • 2012-08-05
      相关资源
      最近更新 更多