• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

writing and reading simple data in XML

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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.

Example:

<user>
<name>john</name>
<location>432</location>
</user>

Thanks,
Lajos
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
lajos kamocsay
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Bill
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
http://www.javaworld.com/javaworld/jw-05-2000/jw-0518-jdom.html

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()

root.addContent(emp);

emp.addContent(id);
emp.addContent(name);
emp.addContent(status);
emp.addContent(salary);
emp.addContent(version);
emp.addContent(edate);

}//while
} 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);
out.flush();
out.close();
} 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
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jdom is best for XML processing
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

Bill
 
Be reasonable. You can't destroy everything. Where would you sit? How would you read a tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic