java - Remove attributes from XMLBean -
assume there's xmlbeans xmlobject attributes, how can selected attributes in single step?
i'm expecting ....
removeattributes(xmlobject obj, string[] selectableattributes){}; now above method should return me xmlobject attributes.
assumption: attributes want remove xmlobject must optional in corresponding xml schema. under assumption, xmlbeans provides couple of useful methods: unsetx , issetx (where x attribute name. so, can implement removeattributes method in way:
public void removeattributes(xmlobject obj, string[] removeattributenames) throws illegalargumentexception, illegalaccessexception, invocationtargetexception, securityexception, nosuchmethodexception { class<?> clazz = obj.getclass(); (int = 0; < 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] + " not optional"); } if (isset != null && isset.booleanvalue() == true) { string unsetmethodname = "unset" + attrname; method unsetmethod = clazz.getmethod(unsetmethodname); unsetmethod.invoke(obj, new object[] {}); } } } note 1: have modified semantics of method signature: second argument (the string[]) list of attributes want remove. think more consistent method name (removeattributes), , simplify things (using unsetx , issetx).
note 2: reason calling issetx before calling unsetx unsetx throw invocationtargetexception if called when attribute x not set.
note 3: may want change exception handling according needs.
Comments
Post a Comment