1

I am running a Powershell script against a view on a Lotus Notes server. It currently produces output in the manner that follows:

UserID|Name|Internet Address
user1|John Smith|john.smith@mycompany.com
user2|Joe Smith|joe.smith@mycompany.com
user3|Bill Smith|bill.smith@mycompany.com

Note: The title line isn't currently part of the output, it is only shown for informational purposes.

The output currently goes to a text file, but I now need to change it to be in XML. The data is extracted using the following Powershell script.

$session = New-Object -ComObject lotus.notessession
$session.initialize()
$session.notesversion

if (Test-Path -Path "c:\myfile.txt") {
    Remove-Item -Path "c:\myfile.txt" 
    }

$db = $session.GetDatabase("lotusServer","names.nsf")
$db.title

$view = $db.GetView('People')
Write-Host "View read : " $view.Name

# Show number of documents 
$nbrDocs = $view.AllEntries.Count 
Write-Host "Num of Docs : " $nbrDocs

# Get First Document in the View 
$doc = $view.GetFirstDocument()

#Read fields for current document 
$delim = "|" 
$i = 0 

    while ($doc -ne $null) {
        $internetAddress = [string] $doc.GetItemValue("InternetAddress") 
        $companyname = [string] $doc.GetItemValue("CompanyName") 
        $fullname = [string] $doc.GetItemValue("FullName")  
        if ( $companyname -eq "My Company") {
            $i = $i + 1
            # format the full name field
            $fullname = $fullname.Substring(3)
            $s = $fullname
            $c1 = $s.LastIndexOf(" ")
            $c2 = $s.IndexOf("/")
            $name = $s.Substring(0,$c2)
            $userID = $s.Substring($c1+1)

            $zOut = $userID + $delim + $name + $delim +  $internetAddress 
            Write-Host $zOut
            Write-Output $zOut| out-file "c:\myfile.txt" -append -width 2000
            } #end if
        $doc = $view.GetNextDocument($doc) 
    }  #end while

I would like the output to now be in an XML format resembling the following:

<?xml version="1.0" ?> 
<Company>
   <UserID>user1
      <Name>John Smith</Name> 
      <InternetAddress>john.smith@mycompany.com</InternetAddress> 
   </UserID> 
</Company>

The data shouldn't have any duplicates, and (at some point) there may be multiple companies in the output but overall the structure wouldn't differ drastically from the above sample (Name and InternetAddress are children of the UserID).

However, the way I'm parsing thru the data now (while $doc -ne $null) I am only seeing one row at a time. Examples I've found showing how to export/write to XML do it at one time & doesn't do it a row at a time. The output XML is going to be validated before being used elsewhere. Can I write the output of this script to an XML file, and if so, how?

Any thoughts/suggestions appreciated.

Thanks!

steve_o
  • 1,243
  • 6
  • 34
  • 60

1 Answers1

3

The following line deals with the record output.

$zOut = $userID + $delim + $name + $delim +  $internetAddress 

You need to modify that so you can write as XML.

Example:

$zOut = "<UserID>" + $userID + "</UserID>" ..... (etc)

You are only seeing one row at a time because you are iterating through a set of records in the view.

If you wanted to get all the data in one go you could use the ?ReadViewEntries&outputformat=XML setting in a view URL lookup. There are limitations on doing this. More details here:

http://publib.boulder.ibm.com/infocenter/domhelp/v8r0/topic/com.ibm.designer.domino.main.doc/H_ABOUT_URL_COMMANDS_FOR_OPENING_SERVERS_DATABASES_AND_VIEWS.html

Alternatively you could craft it in an XPage, or Export to DXL and convert it via XSLT.

But these alternatives are going to be more work then what you have now.

Simon O'Doherty
  • 9,259
  • 3
  • 26
  • 54
  • Thank you - both are very helpful. I had tried with the ReadViewEntries, but there wasn't a view available that had only those fields in it. – steve_o Mar 06 '13 at 21:02