0

属性を持つXMLBeans XmlObjectがあるとします。選択した属性を 1 ステップで取得するにはどうすればよいですか?

なんか期待してしまう……。

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

上記のメソッドは、XMLObjectこれらの属性のみを含む を返すはずです。

4

3 に答える 3

1

前提: から削除する属性XmlObjectは、対応する XML スキーマでオプションである必要があります。この仮定の下で、XMLBeans はいくつかの便利なメソッドを提供します: unsetXand isSetX( whereは属性名です。したがって、次の方法でメソッドを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: メソッド シグネチャのセマンティクスを少し変更しました。2 番目の引数 ( String[]) は、実際には削除する属性のリストです。removeAttributesこれはメソッド名 ( )unsetXとより一貫していると思いますisSetX

isSetX注 2: 呼び出す前に呼び出す理由はunsetX、属性が設定されていない場合に if が呼び出されるためunsetXです。InvocationTargetExceptionX

注 3: 必要に応じて例外処理を変更することもできます。

于 2011-02-28T11:00:03.473 に答える
1

カーソルを使用できると思います...扱いが面倒ですが、リフレクションもそうです。

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;
}
于 2011-10-26T15:30:09.483 に答える
0

この単純な方法を使用して、要素内のすべてをクリーンアップしています。属性のみを削除するには、 cursor.removeXmlContentsを省略できます。2 番目のカーソルは、最初の位置に戻るために使用されます。

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();
}
于 2016-01-12T15:37:44.063 に答える