用于搜索和解析搜索结果的样本代码

使用样本代码和数据图,通过使用 search() 方法来进行搜索、构建搜索过滤器以及解析搜索结果。

此样本代码片段中涵盖以下步骤:

  1. 构建搜索过滤器。
  2. 调用 search() 方法并解析搜索结果。

必备条件

确保您已阅读了“编程先决条件”主题中的信息并完成了其中描述的步骤。

样本代码

请将以下样本代码片段添加到您的应用程序代码,并将变量替换为您要使用的实际值。

/**
 *  This test builds a search filter, calls a search API,
 *  and parses the search results
 */
@SuppressWarnings("unchecked")
public static void testSearch() throws Exception
{
    DataObject root = SDOHelper.createRootDataObject();    DataObject searchCtrl = SDOHelper.createControlDataObject(root, null, SchemaConstants.DO_SEARCH_CONTROL);
    // Set the properties that need to be retrieved in search results
    searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("uid");
    searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("cn"); 
    searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("sn"); 
    searchCtrl.getList(SchemaConstants.PROP_PROPERTIES).add("telephoneNumber");  
    // Set the search base in which search will be performed
    searchCtrl.getList(SchemaConstants.PROP_SEARCH_BASES).add("o=SalesPerson,dc=yourco,dc=com");
    // Set the search filter as uid, here the expression will search for all uids
    searchCtrl.setString(SchemaConstants.PROP_SEARCH_EXPRESSION, "@xsi:type='PersonAccount' and uid='*'");
    System.out.println("Input datagraph before searching for users in the searchbase"+ printDO(root));  
    DataObject result = service.search(root);
    System.out.println("Output datagraph after searching for users in the searchbase"+ printDO(result));
    // Loop through the search results and print the uniqueName
    printIdentifiers(result);
}

输入和输出数据图

用于在搜索条件中搜索用户的输入数据图:
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:controls xsi:type="wim:SearchControl" expression="@xsi:type='PersonAccount' and uid='*'">
      <wim:properties>uid</wim:properties>
      <wim:properties>cn</wim:properties>
      <wim:properties>sn</wim:properties>
      <wim:properties>telephoneNumber</wim:properties>
      <wim:searchBases>o=SalesPerson,dc=yourco,dc=com</wim:searchBases>
    </wim:controls>
  </wim:Root>
</sdo:datagraph>
在搜索条件中搜索用户后的输出数据图:
<?xml version="1.0" encoding="UTF-8"?>
<sdo:datagraph xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sdo="commonj.sdo" xmlns:wim="http://www.ibm.com/websphere/wim">
  <wim:Root>
    <wim:entities xsi:type="wim:PersonAccount">
      <wim:identifier externalName="uid=SalesPerson3,o=SalesPerson,dc=yourco,dc=com" 
          repositoryId="ldapRepo" uniqueId="f3fe184d-e51a-4d02-a5c2-4e2e79e79fea" 
          uniqueName="uid=SalesPerson3,o=SalesPerson,dc=yourco,dc=com"/>
      <wim:uid>SalesPerson3</wim:uid>
      <wim:cn>SalesPerson3cn</wim:cn>
      <wim:sn>SalesPerson3sn</wim:sn>
    </wim:entities>
    <wim:entities xsi:type="wim:PersonAccount">
      <wim:identifier externalName="uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com" 
          repositoryId="ldapRepo" uniqueId="e0db3534-a0ec-4c8b-bfff-a399895a7ca3" 
          uniqueName="uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com"/>
      <wim:uid>SalesPerson1</wim:uid>
      <wim:cn>SalesPerson1cn</wim:cn>
      <wim:sn>SalesPerson1sn</wim:sn>
      <wim:telephoneNumber>11111111</wim:telephoneNumber>
    </wim:entities>
    <wim:entities xsi:type="wim:PersonAccount">
      <wim:identifier externalName="uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com" 
          repositoryId="ldapRepo" uniqueId="7130a46b-d896-4bdd-8f2c-04d9374908c6" 
          uniqueName="uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com"/>
      <wim:uid>SalesPerson2</wim:uid>
      <wim:cn>SalesPerson2cn</wim:cn>
      <wim:sn>SalesPerson2sn</wim:sn>
      <wim:telephoneNumber>22222222</wim:telephoneNumber>
    </wim:entities>
  </wim:Root>
</sdo:datagraph>
对搜索结果进行循环并打印唯一名称:
UniqueName is  -> uid=SalesPerson3,o=SalesPerson,dc=yourco,dc=com
UniqueName is  -> uid=SalesPerson1,o=SalesPerson,dc=yourco,dc=com
UniqueName is  -> uid=SalesPerson2,o=SalesPerson,dc=yourco,dc=com


使用条款 | 反馈