Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa XML XML in database

Have Programming Question? Ask it here!
 
 
 


prashant
XML in database
1 Answer(s)      4 years ago
Posted in : XML


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

Advertisement
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 - XML
xml 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 - XML
storing 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 - XML
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
database data in xml format
database 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 database
XML parsing to Mysql database  Can someone please post the code for parsing an XML file into Mysql database using SAX
How to convert XML file to database ?
How to convert XML file to database ?  How to convert XML file to database
Store XML file into Sqlserver Database
Store 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
retrieving xml document from database
retrieving 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 JS
simple 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 - XML
complex 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
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database?  I want to store extracted xml values in a database... How can i store extacted xml values in a database... give me a example
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database?  I want to store extracted xml values in a database... How can i store extacted xml values in a database... give me a example
converting an xml file in relational database objects
converting an xml file in relational database objects  converting an xml file in relational database objects
Store XML file into Sqlserver Database
Store 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
data insertion from xml file to database table
data insertion from xml file to database table  Hi all, I have data in the XML file. I need to insert it into table in the database using servlet. so please reply me . ThankYou
Insert XML file data to database
Insert 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
Solution to get vaues for xml tags fform sql database in java
Solution to get vaues for xml tags fform sql database in java  hi I have written a code in java to get these xml tags < company comp:loc=" "/>...(" "). how do i get these values.. already connected database and created xml tags
Want solution to get values for xml namespace tags from database in java
Want solution to get values for xml namespace tags from database in java  hi I have written a code in java to get these xml tags < company comp:loc=" "/> now i need to get values from sql database for values in the blank
Data needs to be gathered in XML file from the database (MySql) using JSP
Data needs to be gathered in XML file from the database (MySql) using JSP ... data regarding particular id from the database table. Data needs to be gathered in XML file from the database (MySql) using appropriate JSP/Java Bean functions
Fetch Records from SQL database and convert into XML file
Fetch Records from SQL database and convert into XML file  Hi Experts...' to 'BBB' 3) Once you Update there immediately then you need to push and output XML file with all records into a single XML with containing as Row's. Please see
pls provide steps to connect mysql database in flex - XML
pls provide steps to connect mysql database in flex  hi,i am new to flex and doing project. i have doubt in how to connect MYSQL database in flex .pls provide the steps . i would be really thankful 2 u. i am loooking
XML - XML
XML  How to convert database to XML file?   Hi Friend, Do you want to store database data into xml file ? Please clarify this. Thanks
pls provide steps to connect mysql database in flex - XML
pls provide steps to connect mysql database in flex  hi, pls provide steps to connect mysql database in flex.pls i am trying from last week still it was not connected .pls provide the step by step procedure.i am looking
Need solution to get values for XML namespace tags from SQL database in Java
Need solution to get values for XML namespace tags from SQL database in Java  hi I have written a code in java to get these xml tags < company comp:loc=" "/> now i need to get values from sql database for values
XML
XML  How i remove a tag from xml and update it in my xml
xml
xml  validate student login using xml for library management system
xml
xml  validate student login using xml for library management system
xml
xml  why the content written in xml is more secure
xml
xml  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
XML
XML  create flat file with 20 records. Read the records using xml parser and show required details
XML
XML  please tell me how i remove one tag out of all similar type of tags in xml
xml
xml  how can i remove white space and next line when i copy stream to xml file
XML
XML  Hi...... Please tel me about that Aren't XML, SGML, and HTML all the same thing? Thanks
xml
xml  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
Create XML file from flat file and data insert into database
Create XML file from flat file and data insert into database... create XML and    data insert into the database.");  ... have developed an application to create xml file from flat file and data
xml
xml  Design an XML to maintain book details to do the following: (i) Separate Data (ii) Exchange Data (iii) Store Data (iv) Create new language
XML
XML  Design an XML to maintain book details to do the following: (i) Separate Data (ii) Exchange Data (iii) Store Data (iv) Create new language
XML - XML
XML  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
XML - XML
XML XSD validation in java  Can anyone help in writing validation for XML XSD in Java
XML - XML
XML  How to run XML program?  Hi Friend, Please visit the following links: http://www.roseindia.net/xml/ http://www.roseindia.net/xml/dom/ Hope that it will be helpful for you. Thanks
xml - XML
friend, XPath : It is a language for finding information in an XML document. It is used to navigate through elements and attributes in an XML document. It is a language for selecting nodes from an XML document. It is used
xml - XML
xml  how to match to xml file?can you give example with java code
xml - XML
xml  hi convert xml document to xml string.i am using below code... = stw.toString(); after i am getting xml string result, like Successxxx profile... Friend, Please visit the following link: http://www.roseindia.net/xml/reading
xml - XML
xml  define parser and xml,in jaxp api how to up load parsers... then the program contains syntax errors. XML is defined as an extensible... information,Examples and Tutorials on Xml visit to : http
xml - XML
xml  hi convert xml file to xml string in java ,after getting the xml string result and how to load xml string result? this my problem pls help..."); FileOutputStream output = new FileOutputStream("new.xml"); ReadXML xml = new
Create XMl dynamically - XML
Create 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
XML - XML
XML  Write a program using SAX that will count the number of occurrences of each element type in an XML document and display them. The document file...)); System.out.print("Enter XML file name:"); String xmlFile = bf.readLine
XML to XML Mapping
XML to XML Mapping  Hi Dear Developer Team, I would like to know how does XML-to-XML and XML-to-database Mapping works? I do not want use any... Database-Table(Source/Target)? With best regards, Fartos
Consolidated XML to individual XML - XML
Consolidated XML to individual XML  How to separate the consolidated XML file to separate the individual XML names, when a user import the consolidated XML file
About XML - XML
About XML  What are possible ways XML used in j2ee WebApplication.Like storing data in to database and for data transfer .Apart from this any uses.... Thanks  Hi, See the different uses of XML at http://roseindia.net
Advertisements
 

 

 

DMCA.com