Parsing XML in Lotus Notes using Java
Windows XP Professional, Lotus Notes Client 6, Domino Server 6
Here I assume you have basic knowledge in Java. If you haven’t had a stint with it yet, it is high time to learn Java.
Create a new database in server Go to File–>Database–>New. Give the file name as test.nsf

Open the database in Domino designer.
Let us create one Java agent. Go to agents under shared code in programmers pane. Create new agent. Select java as agent type in drop-down list. In agent properties enter Order Processing as Name. In Run-time drop-down select Action menu selection and All selected documents under Target drop-down list.
Now put the following code snippet as agent code.
import lotus.domino.*; import java.util.*; public class JavaAgent extends AgentBase { public void NotesMain() { try { Session session = getSession(); AgentContext agentContext = session.getAgentContext(); // (Your code goes here) Database db=agentContext.getCurrentDatabase(); //get reference to documents with xml attachments DocumentCollection collection=agentContext.getUnprocessedDocuments(); Document doc=collection.getFirstDocument(); while(doc!=null){ RichTextItem body=(RichTextItem)doc.getFirstItem("Body"); //get reference to xml attachment Vector objects=body.getEmbeddedObjects(); for(int i=0; i<objects.size(); i++){ EmbeddedObject emo = (EmbeddedObject)objects.get(i); if(emo.getType()==EmbeddedObject.EMBED_ATTACHMENT){ //parse xml attahcment and create dom Document object org.w3c.dom.Document document=emo.parseXML(false); //get reference to root element SalesOrder org.w3c.dom.Element rootElement=document.getDocumentElement(); //get reference to order child nodes org.w3c.dom.NodeList orders=rootElement.getElementsByTagName("Order"); for(int j=0; j<orders.getLength(); j++){ org.w3c.dom.Node order=orders.item(j); //create notes order document Document orderDoc=db.createDocument(); orderDoc.replaceItemValue("Form", "Order"); //get reference to all order details: order id, item and quantity org.w3c.dom.NodeList orderDetails=order.getChildNodes(); for(int k=0; k<orderDetails.getLength(); k++){ //replace field values with order details org.w3c.dom.Node orderDetail=orderDetails.item(k); if(orderDetail.getNodeName().equals("Id")) orderDoc.replaceItemValue("OrderId", orderDetail.getFirstChild().getNodeValue()); else if(orderDetail.getNodeName().equals("Item")) orderDoc.replaceItemValue("Item", orderDetail.getFirstChild().getNodeValue()); else if(orderDetail.getNodeName().equals("Quantity")) orderDoc.replaceItemValue("Quantity", orderDetail.getFirstChild().getNodeValue()); } orderDoc.save(false, false); } } } doc=collection.getNextDocument(doc); } } catch(Exception e) { e.printStackTrace(); } } }
Save the agent. This agent is responsible for parsing XML file and creating Notes documents.
Create new form. In form properties set the Name as File Attachment. Create a field. In field properties set the name as Body. Select the type as Rich Text, Editable.
Save the form.
Create another form named Order. Create a table with three rows and two columns. In table properties set the width of cell as 2.0. Now enter text in left cells for field labels. Type the following as labels from top to bottom in left cells.
- Order ID
- Item
- Quantity
Now create fields in the following order from top to bottom in right cells.
- OrderId (Text)
- Item (Text)
- Quantity (Text)
Save the form.
Now we have two forms, one with single rich text field which is used to store XML file and another form used to create Sales Order request documents.
Let us create two views, one for File Attachment document and another for Order documents.
Go to views in programmers pane and create new view. In Create View dialog box, write the view name as Attachments. Select the view type as shared which is default. Copy style from view, select as Blank. Select ‘Select by formula’ check box. Write the following formula in Selection conditions box.
SELECT Form="File Attachment"
Click OK. The view is created. Open the view and create one column with column title as blank and column value formula as File Attachment.
Create a view action. Name it as Process Order. Write the formula as
@Command([ToolsRunMacro]; "Order Processing")
This action is used to trigger Order Processing agent which we have written earlier. Save the view.
Create another view named Orders like Attachments view. Write the selection formula as
SELECT Form="Order"
Create 3 columns each representing OrderId, Item and Quantity fields. You may give column titles as Order Id, Item and Quantity and column field values as OrderId, Item and Quantity.
Save the view.
Ok, now our database is ready with 2 forms, 2 views and one agent.
We need to create one XML file representing order requests. Copy and paste the following lines into your favourite text editor and save the file as order.xml in your machine.
<?xml version="1.0" encoding="utf-8"?> <SalesOrder> <Order> <Id>1</Id> <Item>Round Chair</Item> <Quantity>4</Quantity> </Order> <Order> <Id>2</Id> <Item>Dining Table</Item> <Quantity>1</Quantity> </Order> </SalesOrder>
XML always start with a declaration representing version number. Optionally you may specify encoding which is uni-code in our case. Each XML document has single root element. <SalesOrder> is the root element. Under root element we have Order child elements. Each Order element consists sub child elements like Id, Item, Quantity etc. If you would like to have basic XML understanding refer the tutorial XML Tutorial
OK, now we are going to attach this file in our Notes database. Open the database in Notes client and go to menu Create–>File Attachment. Put your cursor in the body field and go to File–>Attach. File dialog box appears. Select order.xml file and click on Create button.
Save the document. Now go to view Attachments. You could see one File Attachment document. Select the document and click on Process Order action button.
Once the agent is executed, go to Orders view and you could see that two documents are created. The agent parses the attached XML document and create order documents. Now let us walk through the agent code.
DocumentCollection collection=agentContext.getUnprocessedDocuments(); Document doc=collection.getFirstDocument();
The above code get reference to selected document in view.
while(doc!=null){ RichTextItem body=(RichTextItem)doc.getFirstItem("Body"); //get reference to xml attachment Vector objects=body.getEmbeddedObjects(); for(int i=0; i<objects.size(); i++){ EmbeddedObject emo = (EmbeddedObject)objects.get(i); if(emo.getType()==EmbeddedObject.EMBED_ATTACHMENT){
Here we iterate through selected documents and get reference to attachments.
org.w3c.dom.Document document=emo.parseXML(false);
The above code parses XML attachment and creates DOM document object. Here we are using DOM API for Java in processing XML document. Now we have to get Order nodes.
//get reference to root element SalesOrder org.w3c.dom.Element rootElement=document.getDocumentElement(); //get reference to order child nodes org.w3c.dom.NodeList orders=rootElement.getElementsByTagName("Order");
Once we get Order nodes we go to their child nodes and iterate through them. Following code represent it.
for(int j=0; j<orders.getLength(); j++){ org.w3c.dom.Node order=orders.item(j); //create notes order document Document orderDoc=db.createDocument(); orderDoc.replaceItemValue("Form", "Order"); //get reference to all order details: order id, item and quantity org.w3c.dom.NodeList orderDetails=order.getChildNodes(); for(int k=0; k<orderDetails.getLength(); k++){ //replace field values with order details org.w3c.dom.Node orderDetail=orderDetails.item(k);
Then we update order document with node values and save the document.
if(orderDetail.getNodeName().equals("Id")) orderDoc.replaceItemValue("OrderId", orderDetail.getFirstChild().getNodeValue()); else if(orderDetail.getNodeName().equals("Item")) orderDoc.replaceItemValue("Item", orderDetail.getFirstChild().getNodeValue()); else if(orderDetail.getNodeName().equals("Quantity")) orderDoc.replaceItemValue("Quantity", orderDetail.getFirstChild().getNodeValue());
For further reference read Java DOM API
cheers













Problem solved. Changed i< to <
I get error >> ‘)’ expected << on line "for (int i = 0; i<objects.size() ; i++){" Any idea why?
Using V8..
You might want to format (indent) your code.