DOM

DOM

Hello....

I'm creating an xml file from java using DOM...
from the database values...
and saving it in a file
But the created xml file is generating all its files in a single line..
But i need to get the next tag in next line
how to get this..??


Thanks in advance
View Answers

May 26, 2010 at 2:56 PM

Hi Friend,

Try the following code:

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

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

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

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

Element element4 = doc.createElement("Email");
root.appendChild(element4);
Text text4 = doc.createTextNode(email);
element4.appendChild(text4);

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)));
bw.write(xmlString);
//bw.flush();
bw.close();
}
public static void main(String args[])throws Exception{
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Class.forName("com.mysql.jdbc.Driver").newInstance();
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root";);
Statement st=con.createStatement();
ResultSet rs= st.executeQuery("select * from employee where id='1'");
if(rs.next()){
createXmlFile(doc,rs.getString("name"),rs.getString("address"),rs.getString("contactNo"),rs.getString("email"));
}
System.out.println("Xml File Created Successfully");
}
}

Thanks









Related Tutorials/Questions & Answers:
DOM
DOM  hello, What is DOM?   hii,ADS_TO_REPLACE_1 DOM is stands for Document object Model. DOM is an interface-oriented Application Programming Interface. We can use it for navigation of the entire document
DOM
DOM  package name for Document class?   Hi Friend, The package is known as dom api.ADS_TO_REPLACE_1 For more information, visit the following links: http://www.roseindia.net/xml/ http://www.roseindia.net/xml/dom
Advertisements
DOM to SAX and SAX to DOM - XML
DOM SAX Parser Java  What is the Difference between SAX and DOM parsers
DOM to SAX and SAX to DOM - XML
Java DOM to SAX and SAX to DOM  Simple API for XML and Document Object Model
javascript dom error-message
javascript dom error-message  javascript dom error-message
DOM - XML
DOM  Hi... I created an xml file through java by using DOM Now..I need to get the prompt for download when i clicked the button... I used downloadaction...for this But it didn't worked.. Its jus viewing the xml file
Dom in java
Dom in java  Exception in thread "main" org.w3c.dom.DOMException: INVALIDCHARACTERERR: An invalid or illegal XML character is specified... Create XML using DOM import java.io.*; import org.w3c.dom.*; import
DOM - XML
DOM  Hello.... I'm creating an xml file from java using DOM... from the database values... and saving it in a file But the created xml file is generating all its files in a single line.. But i need to get the next tag
ModuleNotFoundError: No module named 'dom'
ModuleNotFoundError: No module named 'dom'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'dom' How to remove the ModuleNotFoundError: No module named 'dom' error
Javascript DOM
generate a fixture list. I was knocking about using DOM techniques but with minimal
dom to xml string
dom to xml string  Hi, I am writing a program in java that reads... have save the dom document into xml file. How to convert dom to xml string... for converting DOM object into string data. Please check the thread dom to xml
Version of batik>batik-dom dependency
List of Version of batik>batik-dom dependency
ModuleNotFoundError: No module named 'ais-dom'
ModuleNotFoundError: No module named 'ais-dom'  Hi, My Python...-dom' How to remove the ModuleNotFoundError: No module named 'ais-dom... to install padas library. You can install ais-dom python with following command
ModuleNotFoundError: No module named 'snag-dom'
ModuleNotFoundError: No module named 'snag-dom'  Hi, My Python... 'snag-dom' How to remove the ModuleNotFoundError: No module named 'snag-dom... to install padas library. You can install snag-dom python with following
ModuleNotFoundError: No module named 'ais-dom'
ModuleNotFoundError: No module named 'ais-dom'  Hi, My Python...-dom' How to remove the ModuleNotFoundError: No module named 'ais-dom... to install padas library. You can install ais-dom python with following command
ModuleNotFoundError: No module named 'dom-query'
ModuleNotFoundError: No module named 'dom-query'  Hi, My Python... 'dom-query' How to remove the ModuleNotFoundError: No module named 'dom... have to install padas library. You can install dom-query python with following
ModuleNotFoundError: No module named 'glance_dom'
ModuleNotFoundError: No module named 'glance_dom'  Hi, My Python... 'glance_dom' How to remove the ModuleNotFoundError: No module named 'glance_dom' error? Thanks   Hi, In your python environment you
Java-XML-DOM - XML
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 dom from string
Java dom from string  Hi, How to create dom tree from string data? I am trying to search the example for java dom from string. Thanks (adsbygoogle = window.adsbygoogle || []).push({});   Hi, Following
DOM API - Java Beginners
DOM API  How can i go through each and every node of an xml file Each and everyChild? my xml file is given below: 810 32 141 22
DOM paring problem
DOM paring problem  Hi My code always show arrayindexoutofbound here is my code i am using arraylist to display and DOM parser to retrieve XML value. I am searching the value by attributes equals 2. this error i am getting
DOM paring problem
DOM paring problem  Hi My code always show arrayindexoutofbound here is my code i am using arraylist to display and DOM parser to retrieve XML value. I am searching the value by attributes equals 2. this error i am getting
DOM API - Java Beginners
DOM API  How can i go through each and every node of an xml file Each and everyChild? my xml file is given below...); } } } ---------------------------------------------- Read for more information. http://www.roseindia.net/xml/dom
Version of batik>batik-svg-dom dependency
List of Version of batik>batik-svg-dom dependency
ModuleNotFoundError: No module named 'py-dom-xpath'
ModuleNotFoundError: No module named 'py-dom-xpath'  Hi, My Python... 'py-dom-xpath' How to remove the ModuleNotFoundError: No module named 'py-dom-xpath' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'py-dom-xpath-six'
ModuleNotFoundError: No module named 'py-dom-xpath-six'  Hi, My... named 'py-dom-xpath-six' How to remove the ModuleNotFoundError: No module named 'py-dom-xpath-six' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'py-dom-xpath'
ModuleNotFoundError: No module named 'py-dom-xpath'  Hi, My Python... 'py-dom-xpath' How to remove the ModuleNotFoundError: No module named 'py-dom-xpath' error? Thanks   Hi, In your python
ModuleNotFoundError: No module named 'py-dom-xpath-six'
ModuleNotFoundError: No module named 'py-dom-xpath-six'  Hi, My... named 'py-dom-xpath-six' How to remove the ModuleNotFoundError: No module named 'py-dom-xpath-six' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'ais-dom-frontend'
ModuleNotFoundError: No module named 'ais-dom-frontend'  Hi, My... named 'ais-dom-frontend' How to remove the ModuleNotFoundError: No module named 'ais-dom-frontend' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'ais-dom-frontend'
ModuleNotFoundError: No module named 'ais-dom-frontend'  Hi, My... named 'ais-dom-frontend' How to remove the ModuleNotFoundError: No module named 'ais-dom-frontend' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'py-dom-xpath-redux'
ModuleNotFoundError: No module named 'py-dom-xpath-redux'  Hi, My... named 'py-dom-xpath-redux' How to remove the ModuleNotFoundError: No module named 'py-dom-xpath-redux' error? Thanks   Hi, In your
ModuleNotFoundError: No module named 'py-dom-xpath-redux'
ModuleNotFoundError: No module named 'py-dom-xpath-redux'  Hi, My... named 'py-dom-xpath-redux' How to remove the ModuleNotFoundError: No module named 'py-dom-xpath-redux' error? Thanks   Hi, In your
Inserting content(DOM Insertion)
Inserting content(DOM Insertion)       Inserting content(DOM Insertion) Inserting Dom contents to html ,can be categories in the following 4 categories
Create an Empty DOM Document
Create an Empty DOM Document       This Example shows how to create an empty DOM Document . JAXP... documents. Here the Document BuilderFactory is used to create new DOM parsers
DOM Parser Tutorial
DOM Parser Tutorial Introduction Dom stands for Document Object Model.... It is available in package org.w3c.dom. Dom is a cross-language API. It provides many... tree elements. With the help of Dom api, user  can add, delete
Maven Dependency batik-dom >> 1.5
You should include the dependency code given in this page to add Maven Dependency of batik >> batik-dom version1.5 in your project
Maven Dependency batik-dom >> 1.6
You should include the dependency code given in this page to add Maven Dependency of batik >> batik-dom version1.6 in your project
Maven Dependency batik-dom >> 1.6-1
You should include the dependency code given in this page to add Maven Dependency of batik >> batik-dom version1.6-1 in your project
Version of com.globalmentor>globalmentor-xml-dom-impl dependency
List of Version of com.globalmentor>globalmentor-xml-dom-impl dependency
Creating DOM Child Elements
Creating DOM Child Elements     ... in the DOM tree. We will first create a blank DOM document and add the root... and the create a blank DOM document. The following code creates a blank document
Pass a dom object from jsp to servlet
Pass a dom object from jsp to servlet  I am creating a dom object in my jsp page. now i want to pass that object in a servlet through calling servlet in jsp. can anyone help me
XML DOM error - Java Beginners
XML DOM error  import org.w3c.dom.*; import javax.xml.parsers.*; import java.io.*; public class DOMCountElement{ public static void main(String[] args) { try { BufferedReader bf = new BufferedReader(new
Java DOM Tutorial
Java DOM Tutorial       This tutorial is complete guide to DOM processing. What is DOM?ADS_TO_REPLACE_1 Document Object Model: DOM is a platform- and language-neutral interface
XML DOM Tutorial
XML DOM Tutorial       W3C provides a standard called DOM (Document Object Model) for HTML & XML documents. DOM is also used as an interface for accessing and manipulating HTML & XML
Document Object Model(DOM)
Document Object Model(DOM)       The Document Object Model (DOM) is a an standard object model... to use DOM in order to render an HTML document. However, JavaScript scripts
Creates a New DOM Parse Tree
Creates a New DOM Parse Tree       This Example describes a method to create a new DOM tree .Methods which are used for making a new DOM parse tree are described below :- Element
The Document Object Model (DOM) APIs
The Document Object Model (DOM) APIs       The DOM Packages  ADS_TO_REPLACE_1  ... This package defines the DOM programming interfaces for XML
Adding an Attribute in DOM Document
Adding an Attribute in DOM Document       This Example shows you how to adds an attribute in a DOM... DOM parsers. There are some of the methods used in code given below for adding
Maven Repository/Dependency: batik | batik-dom
Maven Repository/Dependency of Group ID batik and Artifact ID batik-dom. Latest version of batik:batik-dom dependencies. # Version Release Date 1 1.6 05 Jan 2006
Maven Dependency batik-svg-dom >> 1.5
You should include the dependency code given in this page to add Maven Dependency of batik >> batik-svg-dom version1.5 in your project

Ads