开发者

Remove attributes from XMLBean

Assume there's an XMLBeans XmlObject with attributes, how can I get selected attributes in single step?

开发者_运维百科I'm expecting like something ....

removeAttributes(XmlObject obj, String[] selectableAttributes){};

Now the above method should return me the XMLObject with only those attributes.


Assumption: the attributes that you want to remove from your XmlObject must be optional in the corresponding XML Schema. Under this assumption, XMLBeans provides you with a couple of useful methods: unsetX and isSetX (where X is your attribute name. So, we can implement a removeAttributes method in this way:

public void removeAttributes(XmlObject obj, 
    String[] removeAttributeNames)
        throws IllegalArgumentException, IllegalAccessException,
        InvocationTargetException, SecurityException, 
        NoSuchMethodException {
    Class<?> clazz = obj.getClass();
    for (int i = 0; i < removeAttributeNames.length; i++) {
        String attrName = 
                removeAttributeNames[i].substring(0, 1).toUpperCase() +
                removeAttributeNames[i].substring(1);
        String isSetMethodName = "isSet" + attrName;

        Boolean isSet = null;
        try {
            Method isSetMethod = clazz.getMethod(isSetMethodName);
            isSet = (Boolean) isSetMethod.invoke(obj, new Object[] {});
        } catch (NoSuchMethodException e) {
            System.out.println("attribute " + removeAttributeNames[i]
                    + " is not optional");
        }

        if (isSet != null && isSet.booleanValue() == true) {
            String unsetMethodName = "unset" + attrName;
            Method unsetMethod = clazz.getMethod(unsetMethodName);
            unsetMethod.invoke(obj, new Object[] {});
        }
    }
}

Note 1: I have slightly modified the semantics of your method signature: the second argument (the String[]) is actually the list of attributes that you want to remove. I think this is more consistent with the method name (removeAttributes), and it also simplify things (using unsetX and isSetX).

Note 2: The reason for calling isSetX before calling unsetX is that unsetX would throw an InvocationTargetException if called when the attribute X is not set.

Note 3: You may want to change exception handling according to your needs.


I think you can use a cursor ... they are cumbersome to handle, but so is reflection.

public static XmlObject RemoveAllAttributes(XmlObject xo) {
    return RemoveAllofType(xo, TokenType.ATTR);
}

public static XmlObject RemoveAllofTypes(XmlObject xo, final TokenType... tts) {
    printTokens(xo);
    final XmlCursor xc = xo.newCursor();

    while (TokenType.STARTDOC == xc.currentTokenType() || TokenType.START == xc.currentTokenType()) {
        xc.toNextToken();
    }

    while (TokenType.ENDDOC != xc.currentTokenType() && TokenType.STARTDOC != xc.prevTokenType()) {
        if (ArrayUtils.contains(tts, xc.currentTokenType())) {
            xc.removeXml();
            continue;
        } 

        xc.toNextToken();
    }

    xc.dispose();

    return xo;
}


I am using this simple method to clean everything in the element. You can omit the cursor.removeXmlContents to only delete attributes. Second cursor is used to return to the initial position:

public static void clearElement(final XmlObject object)
{
    final XmlCursor cursor = object.newCursor();
    cursor.removeXmlContents();
    final XmlCursor start = object.newCursor();
    while (cursor.toFirstAttribute())
    {
        cursor.removeXml();
        cursor.toCursor(start);
    }
    start.dispose();
    cursor.dispose();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜