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
XmlObjectmust be optional in the corresponding XML Schema. Under this assumption, XMLBeans provides you with a couple of useful methods:unsetXandisSetX(whereXis your attribute name. So, we can implement aremoveAttributesmethod in this way: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 (usingunsetXandisSetX).Note 2: The reason for calling
isSetXbefore callingunsetXis thatunsetXwould throw anInvocationTargetExceptionif called when the attributeXis not set.Note 3: You may want to change exception handling according to your needs.