JavaAgent XML/Bean Handshake


//...
// DashboardChartJavaAgent (JavaAgent.java)
//...
//begin class
public class JavaAgent extends AgentBase {

//open method, this actually runs the whole App
	public void NotesMain() {

		//let's add a try catch here, grab errors near the end
		try {
			//open our session...
			Session session = getSession();
			//load info console for debugging purposes
			System.out.println("We've got a session..." + session);
			//load agentContext, grab database in question
			AgentContext agentContext = session.getAgentContext();			
			Database currdb = agentContext.getCurrentDatabase();
			
			//load info console for debugging purposes
			System.out.println("connected to database..." + currdb);
			//load view in question
			View view = currdb.getView("SiteList");
			//load info console for debugging purposes
			System.out.println("We've got our view!" + view);
			//load first document
			Document doc;
			Document tempDoc;
			doc = view.getFirstDocument();
			
			//...
			PrintWriter pw = getAgentOutput();
			//...
			pw.println("Content-type:text/xml");
			pw.println("");
			//Write out the Opening Tags
			pw.println("<?xml version='1.0' encoding='UTF-8' ?>");
			pw.println("<PieDataset>");
			//loop though docs and fill xml tags
			while (doc != null) {
				
					//...
					pw.println("<item>");
					
					pw.println("<key>");
					pw.println(doc.getItemValueString("SiteName"));
					pw.println("</key>");
					
					pw.println("<value>");
					pw.println(doc.getItemValueString("SiteNumber"));
					pw.println("</value>");
					
					pw.println("</item>");
			
					tempDoc = view.getNextDocument(doc);
					//recycle, free up memory...
					doc.recycle();
					//...
					doc = audoc;
				}
			//close the root tag
			pw.println("</PieDataset>");
						
			} catch(Exception e) {
				e.printStackTrace();
			}
	}
}







//...
//...
//XMLURL2FileCreate.java

package com.dokoll.solutions.inc.xml.parsers;

//load imports
import java.net.*;
import java.io.*;
import javax.faces.context.FacesContext;
import lotus.domino.local.Database;

/**
 * @author Dököll Solutions, Inc.
 * @version 2012.04.08.3.50.AM
 *
 */




public class XMLURL2FileCreate {


	public void GetXMLFromURL() {
		try {

			//TO DO: Send ip and other info to back-end if page firing
			//via button code by multiple users...

			//Get XML file from URL.
			URL xmlURL = new URL("http", "localhost", 80,
					"/filebin.nsf/dashboardchartdata.xml");
			// Set up Connection to URL, grab file
			URLConnection connectXML = xmlURL.openConnection();
			// connect to URL.... grab file
			connectXML.connect();

			// Build URL file into new XML file
			// ...
			PrintWriter prWriter = new PrintWriter(new FileWriter(
					"c:\\temp\\XML_DATA\\documentfromurl.xml"));
			// Read URL data into new XML file
			BufferedReader bufferRead = new BufferedReader(
					new InputStreamReader(connectXML.getInputStream()));
			// ...
			String dataRead = bufferRead.readLine();

			// ...
			while (dataRead != null) {
				prWriter.println(dataRead);
				dataRead = bufferRead.readLine();

			}

			//close writer...
			prWriter.close();

		} catch (Exception e) {
			System.out.println("Error: " + e);

		}

	}

}
All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.
1 comment(s)Login first to comment...
Stephan H. Wissel
(at 10:06 on 11.04.2012)
If you have more elaborate XML or you might have forbidden chars in your data, you want to use SAX to create the output. Instructions here: http://www.wissel.net/blog/d6plinks/SHWL-8B3G7U