File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes xml creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Head First Android this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "xml creation" Watch "xml creation" New topic

xml creation

Shyam Sunder

Joined: Dec 23, 2006
Posts: 24
Freinds this is my java to xml source code and iam using dom4j.jar



import org.dom4j.Document;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;


public class XMLFileCreator {
public XMLFileCreator(String strFilePath) {

boolean bFileCreation = false;

bFileCreation = createFile(strFilePath);


public Document createXMLDocument() {

Document document = null;

Element root = null;

Element childElem = null;
document = DocumentHelper.createDocument();

root = document.addElement("DBVALUE");
for (int p = 0; p < 5; p++) {

childElem = root.addElement("ADDRESS", "" + p).addAttribute("NAME", "Name").addAttribute("FNAME", "Name-F" + p).addAttribute("LNAME", "Name-L" + p);

childElem = root.addElement("ADDRESS", "" + p).addAttribute("VALUE", "Value-" + p);


return document;


public boolean createFile(String strFilePath) {

boolean bFileCreated = false;

Document document = null;

try {

XMLWriter writer = new XMLWriter(new FileWriter(strFilePath));

document = createXMLDocument();


bFileCreated = true;

} catch (IOException e) {



return bFileCreated;


public static void main(String[] args) {

new XMLFileCreator("D:/EclipseProjects/files/my.xml");



by using this code i want to create below xml file.
but in the above program values are hard coded. But in my case values may change (except headings). For heading i have to use properties file(struts).
but i don't know how to use properties file
and how to iterate values by using arraylist or any hashmap. just modify the above file and so that i can use the code.

<?xml version="1.0" encoding="utf-8" ?>
- <cdrack to="26-MAY-2004">
<fdate from="21-MAY-2004" to="26-MAY-2004" />
- <information>
<headings col1="BRANCH" col2="BASENO" col3="CUSTOMER NAME" col4="VOLUME ACCOUNT NO" col5="OPENING DATE" />
<field BRANCH="600" BASE_NO="8010070459" CUSTOMER_NAME="B BANIF SA" ACCOUNT_NO="70325368" OPENING_DATE="2003-07-26" />
<field BRANCH="600" BASE_NO="8010070459" CUSTOMER_NAME="BAN BN BANIF SA" ACCOUNT_NO="70325376" OPENING_DATE="2003-07-26" />
<field BRANCH="900" BASE_NO="8010070459" CUSTOMER_NAME="SUNDER" ACCOUNT_NO="9989" OPENING_DATE="2006-07-22" />

Thanks & Regards
Neerav Narielwala
Ranch Hand

Joined: Dec 08, 2006
Posts: 106
I think you use jsse.jar and sax2.jar in your java classpath

<a href="" target="_blank" rel="nofollow">Java Tutorials</a> | <a href="" target="_blank" rel="nofollow">Java Weblog</a> | <a href="" target="_blank" rel="nofollow">Computing Articles</a>
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: xml creation
It's not a secret anymore!