读书人

用JDOM的同名标签如何删除?

发布时间: 2012-01-24 23:11:55 作者: rapoo

用JDOM的同名标签怎么删除?急
<locales>
<locale language-country= "zh_CN ">
<pair label-key= "1111 " label-value= "aaaa " />
<pair label-key= "2222 " label-value= "bbbb " />
<pair label-key= "3333 " label-value= "dddd " />

</locale language-country= "zh_CN ">

</locales>


如上面的XML文件 用JDOM读成DOC对象后怎么删除 比如我要删除

<pair label-key= "2222 " label-value= "bbbb " />

这个Element 怎么删除?


[解决办法]
在JDOM的API中(可能存在版本不一致的问题)
1./////////
removeChild
public boolean removeChild(Element element)
This will remove the specified Element.

Parameters:
child - Element to delete.
Returns:
boolean - whether deletion occurred.
2.///////////
public boolean removeChild(java.lang.String name)
This will remove the Element with the specified name. If multiple Elements exist with that name, the first matching child is removed.

Note:Currently, this does not assume that you want the default namespace. I can perform removeChild( "foo:bar "), and it does the right thing. But if I have a document with elements <foo:bar> as well as <bar> , this currently removes the first of those when removeChild( "bar ") is invoked. Do we want to explicitly enforce the default namespace (none) if no prefix is given? (brett)

Parameters:
name - String name of child to delete.
Returns:
boolean - whether deletion occurred.

读书人网 >Eclipse开发

热点推荐