如何处理NamingEnumeration<;搜索结果>;类型(基本Java、LDAP)

如何处理NamingEnumeration<;搜索结果>;类型(基本Java、LDAP),java,casting,ldap,ldap-query,Java,Casting,Ldap,Ldap Query,我想在LDAP服务器中查找数据。当我使用下面的代码时,它希望我有NamingEnumeration(而不是List,HashMap),并且它还强制我使用SearchResult类型 NamingEnumeration<SearchResult> values = dirContext.search("cn=Loggers,cn=config", "(objectClass=*)", searchCtls); namingumeration值= search(“cn=Loggers,

我想在LDAP服务器中查找数据。当我使用下面的代码时,它希望我有NamingEnumeration(而不是List,HashMap),并且它还强制我使用SearchResult类型

NamingEnumeration<SearchResult> values =
dirContext.search("cn=Loggers,cn=config", "(objectClass=*)", searchCtls);
namingumeration值=
search(“cn=Loggers,cn=config”,“objectClass=*)”,searchCtls);
当我尝试使用它时,因为它是namingumeration类型,我不知道如何将其更改为String。有没有办法把它抛到弦上?我想使用split(),但它不是字符串,因此似乎不起作用

for (NamingEnumeration<SearchResult> ne : searchResult) {
                String a = searchResult.split("");   // I want to split.
                if(a.length-1].equals("Logger")){
                String logType = a[a.lenth-2];
                try { 
                     // and then , I will do something with logType
for(NamingEnumeration ne:searchResult){
字符串a=searchResult.split(“”;//我想拆分。
如果(a.length-1).等于(“记录器”)){
字符串logType=a[a.lenth-2];
试试{
//然后,我将使用logType做一些事情

正如你所知,我的Java basic真的很弱。如果有很多方法,我想知道关于如何将NamingEnumeration类型更改为String的建议,我将不胜感激。

迭代NamingEnumeration的常用方法是使用和

namingumeration结果=
search(“cn=Loggers,cn=config”,“objectClass=*)”,searchCtls);
while(results.hasMore()){
SearchResult=results.next();
Attributes=result.getAttributes();
属性cn=attributes.get(“cn”);
//获取/迭代属性的值
}

“增强的for语句(有时称为“for-each循环”语句)”不能用于它们,因为它们实现而不是-接口。原因主要是历史性的,
NamingEnumeration
自Java 1.3以来就存在,
Iterable
自Java 1.5以来就存在。

NamingEnumeration
中迭代值的可能副本,请参阅获取
搜索中的属性值结果
@Hulk感谢您的链接。一个链接帮助我获得了一个“cn”值。我仍然坚持使用NamingEnumeration,似乎对我没有帮助。谢谢。我想您让我完成了一半以上。我必须使用javax.naming.directory.Attribute而不是Attribute(我尽量不使用未绑定的SDK)最后一个问题。我仍然需要将cn变量转换为字符串,因为我想拆分它。cn现在是属性类型。如何将其转换为字符串值。我想执行类似于'cn.split(“”)的操作
NamingEnumeration<SearchResult> results = 
        dirContext.search("cn=Loggers,cn=config", "(objectClass=*)", searchCtls);

while (results.hasMore()) {
    SearchResult result = results.next();
    Attributes attributes = result.getAttributes();
    Attribute cn = attributes.get("cn");
    //get/iterate the values of the attribute
}