从 XMLBean 中删除属性
假设有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设:要从
XmlObject
中删除的属性在相应的 XML 架构中必须是可选的。在这种假设下,XMLBeans 为您提供了一些有用的方法:unsetX
和isSetX
(其中X
是您的属性名称。因此,我们可以以这种方式实现removeAttributes
方法:注 1:我稍微修改了方法签名的语义:第二个参数(
String[]
)实际上是我认为这与方法名称(removeAttributes
)更加一致,并且它也简化了事情(使用unsetX
和isSetX
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
andisSetX
(whereX
is your attribute name. So, we can implement aremoveAttributes
method 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 (usingunsetX
andisSetX
).Note 2: The reason for calling
isSetX
before callingunsetX
is thatunsetX
would throw anInvocationTargetException
if called when the attributeX
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.
我正在使用这个简单的方法来清理元素中的所有内容。您可以省略 cursor.removeXmlContents 以仅删除属性。第二个光标用于返回到初始位置:
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: