从 XMLBean 中删除属性

发布于 2024-10-19 12:36:35 字数 401 浏览 4 评论 0原文

假设有一个 XMLBeans XmlObject 带有属性,如何一步获取选定的属性?

我期待着类似的事情......

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

现在上面的方法应该返回仅包含这些属性的 XMLObject

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还不是爱你 2024-10-26 12:36:35

假设:要从 XmlObject 中删除的属性在相应的 XML 架构中必须是可选的。在这种假设下,XMLBeans 为您提供了一些有用的方法:unsetXisSetX(其中 X 是您的属性名称。因此,我们可以以这种方式实现 removeAttributes 方法:

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[] {});
        }
    }
}

注 1:我稍微修改了方法签名的语义:第二个参数(String[])实际上是我认为这与方法名称(removeAttributes)更加一致,并且它也简化了事情(使用unsetXisSetX

unsetX 之前调用 isSetX 的原因是 unsetX 会抛出 InitationTargetException。如果在未设置属性 X 时调用。

注意 3:您可能希望根据需要更改异常处理。

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.

野生奥特曼 2024-10-26 12:36:35

我认为你可以使用光标......它们处理起来很麻烦,但反射也是如此。

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 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;
}
原野 2024-10-26 12:36:35

我正在使用这个简单的方法来清理元素中的所有内容。您可以省略 cursor.removeXmlContents 以仅删除属性。第二个光标用于返回到初始位置:

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();
}

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文