wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How do i read .ldif file through java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do i read .ldif file through java?" Watch "How do i read .ldif file through java?" New topic
Author

How do i read .ldif file through java?

R . Ravi Kumar
Greenhorn

Joined: Sep 25, 2004
Posts: 6
Hi,

I want to read the data from .ldif file using java. how can i read it. The contents of .ldif file are like:

dn: cn=ravi ravi,mail=xyz@yahoo.com
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
objectclass: mozillaAbPersonAlpha
givenName: ravi
sn: ravi
cn: ravi ravi
mozillaNickname: ravi
mail: xyz@yahoo.com
modifytimestamp: 0Z
...
In this i want to get the value mail attribue. how can i get that value? Please help.

Thanks in advance.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
Quickest way might be to read each line in the usual fashion and look for the line beginning mail:

It would be better to put the whole lot into a Map like a properties file but that won't work because there are five lines with the same first part "objectclass".
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

java.util.Properties supports : as well as = as separator.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
But will properties support several objectclass keys?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Hadn't seen those, and the answer is no.

So reading line for line, then splitting at :

Apache Commons Collections has an interface called MultiMap that could store it all. Alternatively, just using a Map<String,List> or Map<String,Collection> could be useful as well.
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

I use OpenLDAP or Apache Directory to read my ldif-files...
[ September 29, 2008: Message edited by: Ove Lindstr�m ]
jijun lx
Greenhorn

Joined: Jul 12, 2011
Posts: 4
a library is provided by apache directory.
maybe the url follows will do a favor to you

http://grepcode.com/snapshot/repo1.maven.org/maven2/org.apache.directory.shared/shared-ldif/0.9.19
sample:

---------
Jijun
Best regards


------
Best regards
Jijun
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38513
    
  23
Welcome to the Ranch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i read .ldif file through java?