This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal



Apr 21, 2012, 7:09 PM
63 Posts

Build CSV file from XML Data - Bean and Xpages

  • Category: Managed Beans in NSF
  • Platform: All
  • Release: 8.5.2
  • Role: Developer
  • Tags: CSV,XML,Xpages,Java
  • Replies: 0
Knowledge Sharing:
 
 
 

Bean/XML/CSV

Build CSV file from XML Data

Xpages Link



System Requirements:

Download Domino Designer 8.5.2 Environment (DDE)

http://www.ibm.com/developerworks/downloads/ls/dominodesigner/



Introduction:

Load XML file from C Drive using Java, create a CSV file.


Disclaimer:

Information contained in the following is presented as is. This tutorial assumes you have basic programming knowledge. All tutorials are based on an Eclipse/Eclipse-based software. Should you need to familiarize yourself with a certain Eclipse environment, prior to continuing this tutorial, please stop now and see our Tutorials page...


Write Java class to create CSV file

At this point we assume Domino Designer 8.5.2 is downloaded/installed and two Java clas has been created. You are going to use an XML file with a Java to create a CSV file with NSF data. Copy and paste below code to your environment, areas of interest have been highlighted for your convenience.



CreateCSVBean.java


[CODE]



/**

*

* @AppName: Docu.nsf

* Program: CreateCSVBean.java

* Created from Copy: 2012.04.15.12.48.AM

* Create CSV File for JasperReports dataSource

*/



package com.dokoll.solutions.inc.developement.Utils;



/**

*

* @author Dököll Solutions, Inc.

* @version 2012.04.15.12.48.AM

*/


import java.sql.*;


//load program


public class CreateCSVBean {

// run program/button code

public void getKeywords() {

// Set up column and data variables

String columnName = "";

String dataOutput = "";


// entering try catch

try {

// set up document read/build

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory

.newInstance();

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

// read the file into the program

Document doc = docBuilder.parse(new File(

"C://temp/XML_DATA/data.xml"));


// normalize text representation

doc.getDocumentElement().normalize();

System.out.println("Root element of the doc is "

+ doc.getDocumentElement().getNodeName());


// ...

//Get count of values per tags

NodeList listOfSites = doc.getElementsByTagName("person");

// perform count of documents retrieved

int totalSites = listOfSites.getLength();

System.out.println("Total number of sites/customers : " + totalSites);


//loop through sites and fill nodes

for (int s = 0; s < listOfSites.getLength(); s++) {


//...

//Set up siteNode to gather data into elements

Node siteNode = listOfSites.item(s);

if (siteNode.getNodeType() == Node.ELEMENT_NODE) {


//...

Element siteElement = (Element) siteNode;


//...

//load to respective elements

NodeList siteNameList = siteElement

.getElementsByTagName("sitename");

Element siteNameElement = (Element) siteNameList.item(0);


//...

//feed values outbound via each element

NodeList textSiteNameList = siteNameElement.getChildNodes();

System.out.println("SiteName : "+ ((Node) textSiteNameList.item(0)).getNodeValue().trim());

//...

//load to respective elements

NodeList siteNumberList = siteElement

.getElementsByTagName("sitenumber");

Element siteNumberElement = (Element) siteNumberList.item(0);


//...

//feed values outbound via each element

NodeList textSiteNumberList = siteNumberElement.getChildNodes();

System.out.println("SiteNumber : "+ ((Node) textSiteNumberList.item(0)).getNodeValue().trim());


// load results to ColumnNames bound for CSV file

String SiteName = (((Node) textSiteNameList.item(0))

.getNodeValue().trim());

String SiteNumber = (((Node) textSiteNumberList.item(0))

.getNodeValue().trim());

// pre-arrange column values for CSV file

columnName = "SiteName" + "," + "SiteNumber" + "\n"; // ...

// load values to columns set up

dataOutput = dataOutput + SiteName + "," + SiteNumber

+ "\n";

// add debug messages

System.out.println("This is for Debugging measures: "

+ columnName + dataOutput);

// ...

// Instantiate file location...

DataOutputStream outBound = new DataOutputStream(

new BufferedOutputStream(

new FileOutputStream(

"c:/temp/CSV_DATA/customersites.csv")));

// write data into columns set up

outBound.writeBytes(columnName + dataOutput);

// ...

//close the writer

outBound.close();


}


}


} catch (SAXParseException err) {

System.out.println("** Parsing error" + ", line "

+ err.getLineNumber() + ", uri " + err.getSystemId());

System.out.println(" " + err.getMessage());


} catch (SAXException e) {

Exception x = e.getException();

((x == null) ? e : x).printStackTrace();


} catch (Throwable t) {

t.printStackTrace();

}


}// end of program


}

Build Xpage file

Full code added below, jump ahead if necessary, areas of interest have been highlighted for your convenience.



xpload2csv.xsp


<?xml version="1.0" encoding="UTF-8"?>

<xp:view xmlns:xp="http://www.ibm.com/xsp/core">




<xp:link escape="true" id="link4"

style="text-align:center;background-position:center center;width:88.0px">

<xp:eventHandler event="onclick"

submit="true" refreshMode="complete" immediate="false"

save="true" id="eventHandler2">

<xp:image style="width:270.0px;height:170.0px;hspace:10.0px;align:center" id="image3" url="http://localhost/FileBin.nsf/DepartmentInventory.jpg"/>

<xp:this.action><![CDATA[#{javascript:CreateCSVBean.getKeywords()}]]></xp:this.action>

</xp:eventHandler></xp:link>

</xp:view>



Conclusion:

You can now build CSV files from XML, data that can be used by your application in the making of charts and other types of reports. This could be a good way to use SQL to perform queries against this file and attempt other tasks


TIP: You will need to reference CreateCSVBean.java in your faces-config.xml file. Also, this item can be used with a current XML creation from URL sample loaded prior.

 
References:


Questions, comments, please post a brief message on our Contact form on the main site. Thank you for coming...

 

This forum is closed to new posts and responses. Individual names altered for privacy purposes. The information contained in this website is provided for informational purposes only and should not be construed as a forum for customer support requests. Any customer support requests should be directed to the official HCL customer support channels below:

HCL Software Customer Support Portal for U.S. Federal Government clients
HCL Software Customer Support Portal