Sample: Retrieving names from the Profiles directory
Added by IBM contributorIBM | Edited by Claudia R Elbourn on June 12, 2015
Rate this article 1 starsRate this article 2 starsRate this article 3 starsRate this article 4 starsRate this article 5 stars is a Java™ program that displays all the people in the Connections Profiles directory whose name matches the given input. It demonstrates how to read an Atom feed that does not require authentication from the Profiles server, and how to process the returned content using XPath.


To run the program, you must have the following software and information:

  • Java JDK 1.5.0 or later
  • IP address of the Profiles server

Installing the sample

To install the sample, complete the following steps:

Save the link to a local directory, such as c:\samples. Name the file,

Note: The file name is case-sensitive.



1. Download the file by right-clicking the following link.
2. Compile the program. For example, from a command prompt, enter the following command:

Running the sample

To run the sample, use the following command:

java PeopleLike <server-address> <input-name>


The following example queries the Profiles directory for users with the name, daryn.

C:\sample>java PeopleLike daryn


Response: HTTP/1.1 200 OK

profiles where name=daryn

Showing first 1 names of 1 found:

Samantha Daryn


The following Java code is the source of the program:


* Sample client for IBM Connections 1.0.2 Profiles Applications Programming

* Interface


* Displays all people in the Profiles directory whose first name or last name

* starts with the input string given.


* Shows how to read an Atom feed that does not require authentication from the

* Profiles server, and process the contents using XPath.


* Sample invocation:


* java PeopleLike Daryn







import java.util.Iterator;

import javax.xml.XMLConstants;

import javax.xml.namespace.NamespaceContext;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.xpath.XPath;

import javax.xml.xpath.XPathConstants;

import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

public class PeopleLike {

public static String path = "atom/";

public static String param = "?name=";


* main -- for command line invocation


* @param args server-address name-to-search


public static void main(String [] args) {

if (args.length == 0) {

System.out.println("Missing Profiles server address");



if (args.length == 1) {

System.out.println("Missing name to search for");



try {

String server = args [0];

if (!server.endsWith("/")) server += "/";

server = "http://"+ server + path + param + \

URLEncoder.encode(args [1], "UTF-8");

System.out.println("Contacting: "+ server);

URL profiles_url = new URL(server);

// Open the URL: throws exception if not found

HttpURLConnection profiles_conn = \



System.out.println("Response: HTTP/1.1 "_

+profiles_conn.getResponseCode()+" "+profiles_conn.getResponseMessage());

// Process the Atom feed in the response content

readResponse(profiles_url.openStream(), args [0]);

} catch (Exception e) {





* Use XPath to process the Profiles Atom feed document


* @param is

* @param base


static void readResponse(InputStream is, String base ) {

try {

DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();


DocumentBuilder docbuilder = docbf.newDocumentBuilder();

Document feed = docbuilder.parse(is, base);

XPathFactory factory = XPathFactory.newInstance();

XPath xpath = factory.newXPath();

PeopleLike pl = new PeopleLike();

xpath.setNamespaceContext( AtomNamespaceContext());

// Count the Atom Entries in the feed

int count = Integer.parseInt(xpath.evaluate("count(//a:entry)", feed));

// Opensearch extension tells us how many profiles are in this feed

// and any subsequent feed pages

int totalResults = \


// Title of the feed

System.out.println("\n"+ xpath.evaluate("/a:feed/a:title/text()", feed));

System.out.println("Showing first "+ count +" names of "\

+ totalResults +" found:");

// Title of each Profile Entry is the person's name

Object result = \

xpath.evaluate("//a:entry/a:title/text()", feed, XPathConstants.NODESET);

NodeList nodes = (NodeList) result;

for (int i = 0; i < nodes.getLength(); i++) {



} catch (Exception e) {





* Defines XML Namespace contexts used by IBM Connections Profiles


public class AtomNamespaceContext implements NamespaceContext {

public String getNamespaceURI(String prefix) {

if (prefix == null) throw new NullPointerException("Null prefix");

else if ("a".equals(prefix)) return "";

else if ("app".equals(prefix)) return "";

else if ("os".equals(prefix)) return "";

else if ("xml".equals(prefix)) return XMLConstants.XML_NS_URI;

return XMLConstants.NULL_NS_URI;


// This method isn't necessary for XPath processing.

public String getPrefix(String uri) {

throw new UnsupportedOperationException();


// This method isn't necessary for XPath processing either.

public Iterator getPrefixes(String uri) {

throw new UnsupportedOperationException();