jQuery in Action, 3rd edition
The moose likes XML and Related Technologies and the fly likes writing and reading simple data in XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "writing and reading simple data in XML" Watch "writing and reading simple data in XML" New topic

writing and reading simple data in XML

lajos kamocsay
Ranch Hand

Joined: Aug 12, 2006
Posts: 37

I have some simple data structures to be written and read XML.
For parsers I looked at SAX and DOM, but I cannot decide which (and why) would work better.
I found very little information on how people actually write XML. Any information welcome.



Frank Carver

Joined: Jan 07, 1999
Posts: 6920
Generally, I'd recommend staying clear of DOM unless you need clever stuff like xpath queries or XSLT transforms.

If the data is really this simple, you could even get away without a "real" parser at all:

And writing out the XML is very simple too:

Is that any use?

Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
lajos kamocsay
Ranch Hand

Joined: Aug 12, 2006
Posts: 37
Thanks for the idea.

I wanted to use an API so in case the data becomes more challanging I could reuse the same code.

Somone actually suggested I use DOM because it's easier to setup than SAX and the performance hit would not be too great for small applications.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
If your files are small, then DOM will be the easiest. Java standard edition since 1.4 has included all you need to parse an XML document and you can count on the API being stable.

For writing, you can get quite complicated, depending on the form your data is already in, but I usually just write with print calls.

Alix Sye

Joined: Jun 07, 2006
Posts: 17
For reading and writing xml files JDOM is the best. You can read and xml file and load it to the database or read a database table and write an xml file from the database. JDOM has all the easy tools to do both

jdom website is http://www.jdom.org/

Once you install the latest API from JDOM website you need to copy the
following JARS in your lib directory
All the jars from the JDOM API.
jdom.jar from the build directory of the installed JDOM API.

Read the following excellent article how to read and write from xml

The following import statements you need for the followeing code to work
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

Following is the sample code for reading xml file and updating the database

sql_stmt = "insert into emp (name, status, salary, version, emp_date) values (?,?,?,?,?)";

try { pstmt = conn.prepareStatement(sql_stmt); }
catch (SQLException ex) { ex.printStackTrace(); }

SAXBuilder builder = new SAXBuilder(false);

try {
Document doc = builder.build("C:/Projects/web/emp.xml");
// Get the root element
Element root = doc.getRootElement();

// Update Employee Information
List emps = root.getChildren("emp");
int emp_size = emps.size();

Iterator i = emps.iterator();
while (i.hasNext()) {
Element emp = (Element) i.next();
String emp_name = emp.getChild("name").getText();
String emp_status = emp.getChild("status").getText();
String emp_salary = emp.getChild("salary").getText();
String emp_version = emp.getChild("version").getText();
String emp_date = emp.getChild("emp_date").getText();

pstmt.setString(1, emp_name);
pstmt.setString(2, emp_status);
pstmt.setString(3, emp_salary);
pstmt.setString(4, emp_version);
pstmt.setString(5, emp_date);

try { rows_affected = MT.P_Transaction(pstmt); } catch (SQLException ex) { ex.printStackTrace(); }
} // while

} catch (JDOMException ex) { ex.printStackTrace(); }
catch (IOException ex) { ex.printStackTrace(); }
catch (SQLException ex) { ex.printStackTrace(); }

try { pstmt.close(); }
catch (SQLException ex) { ex.printStackTrace(); }

Following is the sample code for reading Database and creating XML file

Element root = new Element("Web-Emp");

// create a class to read the data from DBMS into a resultset
// MT.Get_Resultset_Emp(conn) is method reading DBMS into Resultset

ResultSet crs_emp = MT.Get_Resultset_Emp(conn);

try {
while (crs_emp.next()) {
int emp_id = crs_emp.getInt("id");
String emp_name = crs_emp.getString("name");
String emp_status = crs_emp.getString("status");
String emp_salary = crs_emp.getString("salary");
String emp_version = crs_emp.getString("version");
String emp_date = crs_emp.getString("emp_date");

Element emp = new Element("emp");
emp.setAttribute("index", String.valueOf(emp_id));

Element id = new Element("id");
id.setText(String.valueOf(emp_id)); //low.toString()
Element name = new Element("name");
name.setText(emp_name); //low.toString()
Element status = new Element("status");
status.setText(emp_status); //low.toString()
Element salary = new Element("salary");
salary.setText(emp_salary); //low.toString()
Element version = new Element("version");
version.setText(emp_version); //low.toString()
Element edate = new Element("emp_date");
edate.setText(emp_date); //low.toString()



} catch (SQLException ex) {ex.printStackTrace(); }

Document doc = new Document(root);
// serialize it into a file
try {
FileOutputStream out = new FileOutputStream("C:/web/fibonacci.xml");
XMLOutputter serializer = new XMLOutputter();
serializer.output(doc, out);
} catch (IOException e) { System.err.println(e); }

The above code is tested and works fine for the following
1- For Updating a database table from reading an XML file.
2- For Creating an XML file by reading a database table.

I hope above code will help all those people who want an easy xml to database processing. JDOM is best API for this purpose.
Alix Sye

Joined: Jun 07, 2006
Posts: 17
Jdom is best for XML processing
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Originally posted by Alix Sye:
Jdom is best for XML processing

Blanket statements like this are not particularly helpful. One persons situation is sure to differ from just about anyone else's. It's better to give reasons why you think JDOM is great compared to the other XML libraries you have used.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
Jdom is best for XML processing

I disagree because the Java standard library contains all you need for most XML processing including XPath. I am not about to require a customer to add the JDOM library to all their systems just to run my Java utility.

At one time there was a suggestion that JDOM would become part of the standard library - see JSR-102 (I think) but nothing has happened yet.

There are MANY attempts at creating toolkits for easier/faster/simpler/whatever processing of XML. In most cases the standard library is fine.

wood burning stoves
subject: writing and reading simple data in XML
It's not a secret anymore!