By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

how to read data from XML file & store it in database(MySql)

P: 2
Hi,

I need to get the data from XML file and store it in MySql database.
How can i do it,could you please tell me.



Regards,
KiranRredy
Dec 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: 10
hi buddy,

ref this page u ll find solution for this :

http://xml.apache.org/xerces-c/


regards

Prakash Ravindran
Dec 19 '06 #2

P: 26
What techniques have you tried already? With java, there are 2 main ways of reading XML documents:

1. DOM - Document Object Model interface. This loads the XML document into main memory. With the Document Object Model, you can build documents, navigate their structure, and add, modify, or delete elements and content. Basically the DOM specifies an abstract datatype for XML logical structure. In Java the API for this is known as JAXP

http://java.sun.com/j2se/1.5.0/docs/...e-summary.html

An example of reading in a doc could be:

Expand|Select|Wrap|Line Numbers
  1. public void readDoc() {
  2.   try {
  3.  
  4.    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  5.    DocumentBuilder builder = factory.newDocumentBuilder();
  6.    doc = builder.parse( new File("myXMLFile.xml") );
  7.  
  8.   }
  9.   catch (FactoryConfigurationError fce){
  10.     System.err.println("Could not create DocumentBuilderFactory");
  11.   }
  12.   catch (ParserConfigurationException pce) { 
  13.     System.out.println("Could not locate a JAXP parser"); 
  14.   }
  15.   catch (SAXException se) {
  16.     System.out.println("XML file is not well-formed.");
  17.   }
  18.   catch (IOException ioe) { 
  19.     System.out.println(
  20.     "Due to an IOException, the parser could not read the XML file"
  21.     ); 
  22.   }
  23. }
  24.  
doc is the object which holds the xml document in.

obviously you'd need to import all relevant packages etc.

Expand|Select|Wrap|Line Numbers
  1.  
  2. import javax.xml.parsers.DocumentBuilder;
  3. import javax.xml.parsers.DocumentBuilderFactory;
  4. import javax.xml.parsers.FactoryConfigurationError;
  5. import javax.xml.parsers.ParserConfigurationException;
  6.  
  7. import org.xml.sax.SAXException;
  8. import org.xml.sax.SAXParseException;
  9.  
  10. import java.io.File;
  11. import java.io.IOException;
  12.  
  13. import org.w3c.dom.*;
  14.  
  15.  
2. The second way in java is to use the SAXP API, which doesn't load the XML file into memory. This is quicker than JAXP, and is useful when you know the structure of the xml file isn't going to change etc.

http://java.sun.com/j2se/1.5.0/docs/...SAXParser.html

Finally, to send it to mySQL, you need to open a connection String to the mySQL database, and then run whatever queries you want. You can use a system ODBC driver, or Sun's own JDBC driver if you haven't got the first one.

This isn't comprehensive, but should give you enough to start.....

mharrison
Dec 19 '06 #3

P: 1
If you are familiar with SQL then you could try using Stels-XML JDBC driver and hibernate together.
I have used the combination successfully on a project.
Stels-XML is a commercial product but it allows you to read an XML file using SQL commands.
Hibernate takes Java objects and writes them to the database by generating the SQL for you.

Links:
Stels Stels-XML
Hibernate hibernate

As with the other solutions you'll also need a JDBC driver.
JDBC driver for MySQL

Best of luck.
Jan 27 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.