xml in java


xml in java

xml in java
2 Answer(s)      7 years ago
Posted in : Java Beginners
how to create a database in xml and then display its content in html table?
View Answers

June 4, 2010 at 3:08 PM

Hi Friend,

Try the following code:

import java.io.*;
import java.sql.*;
import java.util.*;
import java.awt.*;
import org.w3c.dom.*;
import javax.swing.*;
import java.awt.event.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class HTMLParser {
public static void createXmlFile(Document doc,String name,String address,String contactNo,String email) throws Exception {
Element root = doc.createElement("Employee");
Element element1 = doc.createElement("Emp_Name");
Text text1 = doc.createTextNode(name);

Element element2 = doc.createElement("Emp_Address");
Text text2 = doc.createTextNode(address);

Element element3 = doc.createElement("Emp_ContactNo");
Text text3 = doc.createTextNode(contactNo);

Element element4 = doc.createElement("Emp_Email");
Text text4 = doc.createTextNode(email);

TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");

StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();

File file = new File("c:/employee.xml");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));
public static void main(String args[])throws Exception{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
String name="",address="",contact="",email="";
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root";);
Statement st=con.createStatement();
ResultSet rs= st.executeQuery("select * from user_details where userid='1'");

June 4, 2010 at 3:09 PM


System.out.println("Xml File Created Successfully");
File file = new File("C:/employee.xml");
doc = docBuilder.parse(file);
NodeList studentList = doc.getElementsByTagName("Employee");
if (studentList != null && studentList.getLength() > 0) {
for (int i = 0; i < studentList.getLength(); i++) {
Node node = studentList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element e = (Element) node;
NodeList nodeList = e.getElementsByTagName("Emp_Name");
nodeList = e.getElementsByTagName("Emp_Address");
nodeList = e.getElementsByTagName("Emp_ContactNo");
nodeList = e.getElementsByTagName("Emp_Email");
String initialText = "<html><table border='1'><tr><th color='red'>Name</th><th color='red'>Address</th><th color='red'>Contact No</th><th color='red'>Email</th></tr><tr><td>"+name+"</td><td>"+address+"</td><td>"+contact+"</td><td>"+email+"</td></table></html>";
JLabel label = new JLabel(initialText);
JPanel p=new JPanel();
JFrame frame = new JFrame("HTMLInSwing");

For the above code, we have created following database table:

CREATE TABLE `user_details` (
`userid` bigint(255) NOT NULL auto_increment,
`name` varchar(255) default NULL,
`address` varchar(255) default NULL,
`contactNo` int(255) default NULL,
`email` varchar(255) default NULL,
PRIMARY KEY (`userid`)



Related Tutorials/Questions & Answers:
java and xml - XML
java and xml  Hi Deepak, I want learn xml and java(applications). Which editor is best usefull(trial--version) in my applications...; Hi friend, http://www.roseindia.net/xml/dom/ Thanks
Java + XML - XML
Java + XML  1) I have some XML files, read one xml... java...the attribute value..not sure now how to read the xml file passing..." pointing to a hello xml file ..read that file and get the value of the parent
Java-XML-DOM  Hi! I need some help. I have to make java program that loads an xml file and from it builds DOM(later i will have to work with it - like using xpath in java find some value and replace it...). Since i'm new to java
java and xml - XML
java and xml   test_final_1 2009-025T13:23:45 B2B I want to validate each tag.. and i don't have any xsd.. kindly help me to solve...://www.roseindia.net/xml/dom/DOMValidateDTD.shtml Thanks
java with xml
java with xml  Hi i am reading xml data with sax parser in java. ok its fine. But in future xsd and xml will change. now my question is if xsd and XML will change my java progrm will not change. is it possible ? Thanks
java with xml
java with xml  Hi i am reading xml data with sax parser in java. ok its fine. But in future xsd and xml will change. now my question is if xsd and XML will change my java progrm will not change. is it possible ? Thanks
document and the processing in JAVA program . Find the time for each XML element...XML with JAVA  Hi.. This is Priya here. Thanks for responding me. I have a query. "Write program in java to evaluate the time to access
java and xml problem - XML
java and xml problem  hi, i need to write a java program that generates an xml file as follows: aaa vvv --------- i have witten a program in java, but im
java - XML
This is my XML file then how can read this XML file Using DOM & SAX parsers in java? How can write the same data into XML file using DOM parser? Could you
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have only one element with multiple attributes with same tag name. here is my file... a solution.my java code is given below. import java.io.File; import
XML in java - XML
XML in java  Write a program using SAX that will count the number of occurrences of each element type in an XML document and display them...://www.roseindia.net/xml/sax/SAXElementCount.shtml Hope that it will be helpful
java with xml
java with xml  hi i have a problem. // this sample code is reading xml file in java DefaultHandler handler = new DefaultHandler... xml file change in future then my java code will also change. but is there any
java - XML
java  How can write data into XML file using DOM parser? How can convert HTWL file to XML file
java - XML
java  how can i validate my xml file using java code plz send me de... kumar  Hi friend, Step to validate a xml file against a DTD (Document Type Definition) using the DOM APIs. Here is the xml file "User.xml
XML and java
XML and java  Hi I have xml data in XMLStreamreader object how to retrive tha data and write into a file using java Your help will be much appreciated Thanks   Hi Friend, Try the following code: import java.io.
java - XML
in the XML document while i am parsing the file using SAX event based parser.... provide the way in java.  Hi Friend, Try the following code: import... parser = parserFact.newSAXParser(); System.out.println("XML Data
java - XML
java  How to prepare an xml document by reading data from a table in the database . (the database access logic is written inside the servlet
java - XML
you get the data3. And in the servlet class you can use the DOM api to create xml... it to the xml treedoc.appendChild(root);For more information please visit http://www.roseindia.net/xml/dom/createdomchildelement.shtmlThanks
java - XML
java  How to read the values of XMLStreamConstants.CDATA in simple java
JAVA  hi.. i want to talk to any SWT expert in JAVA... how can i do it?   Hi friend, For read more information,Examples and Tutorials on SWT visit to : http://www.roseindia.net/tutorials/swt/ Thanks
XML parsing using Java - XML
XML parsing using Java  I'm trying to parse a big XML file in JAVA. The goal is like i will take console input from user until "Coverage" in element "MIRate"(THE XML code is below).once he enters coverage rate I need
xml to html via java
xml to html via java  how to read xml into html using java code
Java XML
Java XML In this section we will learn how to manipulate XML files in Java program. Java provides excellent API for manipulating the XML files. In your Java... the XML files. API's for XML parsing in Java: DOM The DOM API
accessing xml using java
accessing xml using java  I need to retrieve some elements in xml file using java   Hi Friend, Please visit the following links: http://www.roseindia.net/xml/Listingnode.shtml http://www.roseindia.net/xml/getting
xml file creation in java
xml file creation in java  how to create xml file in java so...; Please visit the following links: http://www.roseindia.net/tutorial/java/xml...;Please visit the following links: http://www.roseindia.net/tutorial/java/xml
XMl and Java web services
XMl and Java web services  I want to do a project on java web services and XML. I wanted to ask, how the data entered by user through the HTML file can be converted to an XML file and sent to the server? At the server site, how
XML in JAVA - Java Beginners
XML in JAVA  hi.. i have an XML file .. and i have parsed... in another file to get all the elements in the documents.. but my java SDK(i.e Eclipse) gives Java Null Pointer exception... can anyone tell me what could be wrong
XML-JAVA - Java Beginners
XML-JAVA  How to create XML file and write to this file in JAVA...,contactNo,email); System.out.println("Xml File Created Successfully...://www.roseindia.net/xml/dom/CreatXMLFile.shtml Thanks
Question on java & XML
Question on java & XML  How do we convert the names of files and subfolders into an xml file... Suggestions needed...what are classes we need to use
Validation on input xml in java program - XML
Validation on input xml in java program  Hi.. i am accepting xml data as input which I have to use in my program. i have to validate this input xml against some schema using xml beans and NOT by using sax or dom parser. I am
java code - XML
java code  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 to be processed should be identified by the first command-line argument