2012-08-28 17 views
6

प्रश्न सब कुछ कहता है। जब मैं एक गुण मुद्रण कर रहा हूँ यह है:javax.naming.directory से मूल्य निकालने के लिए कैसे करें।

cn: WF-008-DAM-PS 

कोड स्निपेट है:

private void searchGroup() throws NamingException { 
    NamingEnumeration<SearchResult> searchResults = getLdapDirContext().search(groupDN, "(objectclass=groupOfUniqueNames)", getSearchControls()); 
    String searchGroupCn = getCNForBrand(m_binder.getLocal("brandId"), m_binder.getLocal("brandName")); 
    Log.info(searchGroupCn); 
    while (searchResults.hasMore()) { 
     SearchResult searchResult = searchResults.next(); 
     Attributes attributes = searchResult.getAttributes(); 
     Attribute groupCn = attributes.get("cn"); 
     if(groupCn != null) { 
      Log.info(groupCn.toString());    
     } 
    } 
} 

कैसे मैं केवल मान प्राप्त कर सकते हैं: WF-008-DAM-PS, कि कुंजी भाग के बिना है? सम्मान।

उत्तर

4

getValue() विधि या getValue(int) विधि को आमंत्रित करें।

+0

इन दोनों तरीकों javax.naming.directory.BasicAttribute या javax.naming.directory.Attribute में मौजूद हैं है (Attribute उदाहरण कई मान हैं जब)? एक विधि (int) है। –

+0

'विशेषता' एक इंटरफ़ेस है, 'BasicAttribute'' विशेषता 'लागू करता है। तो, 'अंतिम ऑब्जेक्ट o = groupCn.getValue() ', मान लीजिए कि' groupCn' एकल मूल्यवान है। यदि यह बहु-मूल्यवान है, तो 'groupCn.getValue (अनुक्रमणिका)' –

+0

पर पैरामीटर के रूप में पूर्णांक अनुक्रमणिका का उपयोग करें धन्यवाद, लेकिन ऐसी विधि नहीं है Value() न तो http://docs.oracle.com/javase/1.4 में। 2/डॉक्स/एपीआई/जावैक्स/नामकरण/निर्देशिका/BasicAttribute.html या http://docs.oracle.com/javase/1.4.2/docs/api/javax/naming/directory/Attribute.html –

6

समाधान है:

Attribute groupCn = attributes.get("cn"); 
String value = groupCn.get(); 
1

जनरल

कहते है कि हम करते हैं:

Attributes attributes; 
Attribute a = attributes.get("something"); 
  • if(a.size() == 1)
    • तो आप a.get() या a.get(0) उपयोग कर सकते हैं सभी मूल्यों के माध्यम से अनूठा मूल्य
  • if(a.size() > 1)

    • पुनरावृति पाने के लिए:

      for (int i = 0 ; i < a.size() ; i++) { 
          Object currentVal = a.get(i); 
          // do something with currentVal 
      } 
      

      आप यहाँ a.get() उपयोग करते हैं, यह वापस आ जाएगी केवल पहला मान, क्योंकि इसका आंतरिक कार्यान्वयन (BasicAttribute में) ऐसा लगता है:

      public Object get() throws NamingException { 
          if (values.size() == 0) { 
           throw new NoSuchElementException("Attribute " + getID() + " has no value"); 
          } else { 
           return values.elementAt(0); 
          } 
      } 
      

दोनों ही तरीकों से (get(int) और get()) एक NamingException फेंकता है।

व्यावहारिक उदाहरण

LdapContext ctx = new InitialLdapContext(env, null); 

Attributes attributes = ctx.getAttributes("", new String[] { "supportedSASLMechanisms" }); 
System.out.println(attributes); // {supportedsaslmechanisms=supportedSASLMechanisms: GSSAPI, EXTERNAL, DIGEST-MD5} 

Attribute a = atts.get("supportedsaslmechanisms"); 
System.out.println(a); // supportedSASLMechanisms: GSSAPI, EXTERNAL, DIGEST-MD5 

System.out.println(a.get()); // GSSAPI 

for (int i = 0; i < a.size(); i++) { 
    System.out.print(a.get(i) + " "); // GSSAPI EXTERNAL DIGEST-MD5 
} 
+0

@Downvoter, कृपया अपने निर्णय के बारे में एक स्पष्टीकरण जोड़ें ... मुझे लगता है कि यह एक बहुत अच्छा जवाब है। –

संबंधित मुद्दे