XML in database
Hi Deepak,
i m again facing problem with single element multiple tag in xml.
i m trying to read the tag values into my database but it gives nullpointerexception. pls guide me appropriatly.
MyXML Structure is ,
Test1.xml
<test1>
<Firsttab>This</Firsttab>
<Firsttab>Is</Firsttab>
<Firsttab>it</Firsttab>
</test1>
and to read this xml into my db(which is in mysql) i write the following code.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
public class Test1 extends Object {
public static void main (String args[]){
String testFile = "Test1.xml";
Document testDoc = null;
try {
DocumentBuilderFactory docbuilderfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docbuilder = docbuilderfactory.newDocumentBuilder();
testDoc = docbuilder.parse(testFile);
System.out.println("File Readed Successfully");
} catch (Exception e) {
System.out.println("Cannot read the test file: "+e.getMessage());
}
//For the JDBC-ODBC bridge, use
//driverName = "sun.jdbc.odbc.JdbcOdbcDriver"
//and
//connectURL = "jdbc:odbc:orders"
String driverName = "com.mysql.jdbc.Driver";
String connectURL = "jdbc:mysql://localhost:3306/training";
Connection db = null;
try {
Class.forName(driverName);
db = DriverManager.getConnection(connectURL,"root","mysql");
System.out.println("Connect Successfullly");
//Get the root element and all order elements
Element testRoot = testDoc.getDocumentElement();
System.out.println(testRoot);
NodeList test = testRoot.getElementsByTagName("test1");
System.out.println(test);
for(int i = 0; i < test.getLength(); i++)
{
//For each order, get the order element
Element thisTest = (Element)test.item(i);
System.out.println(thisTest);
//Get name
String thisTestname = thisTest.getElementsByTagName("Firsttab").item(0).getFirstChild().getNodeValue();
System.out.println("Firsttab: " + thisTestname);
System.out.println();
}
}
catch (ClassNotFoundException e)
{
System.out.println("Error creating class: "+e.getMessage());
} catch (SQLException e) {
System.out.println("Error creating connection: "+e.getMessage());
} finally {
System.out.println("Closing connections...");
try {
db.close();
} catch (SQLException e) {
System.out.println("Can't close connection.");
}
}
}
}
please give me a solution.
Thanks,
Prashant
View Answers
July 22, 2010 at 5:31 PM
Hi Friend,
Try the following code:
import javax.xml.parsers.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.sql.*;
public class xml{
public static void main(String[] args) {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:
mysql://localhost:3306/register";, "root", "root");
Statement st=connection.createStatement();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("test.xml");
NodeList n1= doc.getElementsByTagName("Firsttab");
for(int i=0;i<n1.getLength();i++){
String st1= n1.item(i).getFirstChild().getNodeValue();
st.executeUpdate("insert into tabs(name) values('"+st1+"')");
}
}
catch(Exception e){
e.printStackTrace();
}
}
}
Thanks
Related Tutorials/Questions & Answers:
xml or database - XMLxml or database If I implement some web applications, which is a better way to retrieve and parse
xml file directly using DOM or storing those
xml files in
Database and retrieve. My
xml files will be about 100 - 200 and each
storing xml into database - XMLstoring
xml into
database hi i have an
xml file it contains... .......with it i know how to persist a simple
xml file into data base but i m finding some difficulty to start with i m sending u the
xml file
Advertisements
XML in database - XMLXML in database Hi Deepak,
i m again facing problem with single element multiple tag in
xml.
i m trying to read the tag values into my
database but it gives nullpointerexception. pls guide me appropriatly.
MyXML
database data in xml formatdatabase data in
xml format HI,
i want to display the
database data in the
xml format(not as
xml file ) on the console using DOM. help will be appreciated.
THANKS
K.K
XML parsing to Mysql databaseXML parsing to Mysql database Can someone please post the code for parsing an
XML file into Mysql
database using SAX
retrieving xml document from databaseretrieving
xml document from database Hi Guys,
I want to retrieve the
xml document stored in the
database on to a jsp page using jdbc/odbc connnection.Please help me in implementing this feature.
Thank You
Madhu
simple code for XML database in JSsimple code for
XML database in JS Sir ,
i want a code in javascript for
XML database to store details (username and password entered by user during registration process (login process)).
please send me a code .
Thank you
complex xml parsing and storing in database - XMLcomplex
xml parsing and storing in database Hi Experts ,i want to parse my
xml document and store it in mysql
database. This is my code. How to parse this complex data.
EDI_DC40
800
0000000000557748
Store XML file into Sqlserver DatabaseStore
XML file into Sqlserver Database Code for storing a typical
XML file into SQl server
database in 1 or more tables using asp.net using C#.
<?
xml version="1.0" ?>
<!DOCTYPE PubmedArticleSet (View Source
Insert XML file data to databaseInsert
XML file data to
database
In this tutorial, you will learn how to insert the
xml file data to
database
using dom parser.
You all are aware of
XML... to read the
xml file data and save the values
of an
XML file to a
Database Fetch Records from SQL database and convert into XML fileFetch Records from SQL
database and convert into
XML file Hi Experts... and output
XML file with all records into a single
XML with containing as Row's.
Please see the output
XML: -
I need output
XML File as: -
**ADS_TO_REPLACE_2
XML - XMLXML How to convert
database to
XML file?
Hi Friend,
Do you want to store
database data into
xml file ?
Please clarify this.
Thanks
XMLXML How i remove a tag from
xml and update it in my
xml xmlxml why the content written in
xml is more secure
xml xml validate student login using
xml for library management system
xml xml validate student login using
xml for library management system
xmlxml what is name space,
xml scema give an example for each
XML Namespaces provide a method to avoid element name conflicts.They are used for providing uniquely named elements and attributes in an
XML document
XMLXML please tell me how i remove one tag out of all similar type of tags in
xml xmlxml how can i remove white space and next line when i copy stream to
xml file
XMLXML create flat file with 20 records. Read the records using
xml parser and show required details
xmlxml what is
xml
Extensible Markup Language (
XML... that is both human-readable and machine-readable. It is defined in the
XML 1.0... gratis open standards.
The design goals of
XML emphasize simplicity, generality
XMLXML Hi......
Please tel me about that
Aren't
XML, SGML, and HTML all the same thing?ADS_TO_REPLACE_1
Thanks
XMLXML Design an
XML to maintain book details to do the following:
(i) Separate Data
(ii) Exchange Data
(iii) Store Data
(iv) Create new language
xmlxml Design an
XML to maintain book details to do the following:
(i) Separate Data
(ii) Exchange Data
(iii) Store Data
(iv) Create new language
databasedatabase the code for initializing the
database connection
databasedatabase i want to let the user select the name of
database and then delete that
database.......im doing project in swings netbeans
databasedatabase use of hyperlink to show the data from
database databasedatabase i need to insert time in my
database. i need a code to insert time in my
database. its very urgent
XML - XMLXML What is specific definatio of
XML?
Tell us about marits and demarits of
XML?
use of
XML?
How we can use of
XML? Hi,
XML... language much like HTML used to describe data. In
XML, tags are not predefined
Create XMl dynamically - XMLCreate
XMl dynamically Hi
I am retreiving the list from
database which i need to display in an
XML file with some nodes
How can I do
databasedatabase im doing my project in netbeans swings...wn a user wants to create a
database from the gui...i want to display an error msg if a
database with that name already exists and if it does not exist new
database should
databasedatabase tell me use about
database and give me a small program.
It is secure and can easily be accessed, managed, and updated. Moreover... links:
Connect JSP with
database Mysql
Connect Java with
database Mysql
databasedatabase tell me use about
database and give me a small program.
It is secure and can easily be accessed, managed, and updated. Moreover... links:
Connect JSP with
database Mysql
Connect Java with
database Mysql
databasedatabase tell me use about
database and give me a small program.
It is secure and can easily be accessed, managed, and updated. Moreover... links:
Connect JSP with
database Mysql
Connect Java with
database Mysql
databasedatabase I wanted to know if it is possible to create a
database in mysql by letting the user enter the name of the
database in swing gui..im doing my project in netbeans...Thank You