XML-JAVA

XML-JAVA

How to create XML file and write to this file in JAVA?
View Answers

February 8, 2010 at 12:05 PM

Hi Friend,

Try the following code:

import java.io.*;
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();
Scanner input=new Scanner(System.in);
System.out.print("Enter Employee Name:");
String name=input.nextLine();
System.out.print("Enter Address:");
String address=input.nextLine();
System.out.print("Enter Contact No:");
String contactNo=input.nextLine();
System.out.print("Enter Email:");
String email=input.nextLine();
createXmlFile(doc,name,address,contactNo,email);
System.out.println("Xml File Created Successfully");
}
}

For more information, visit the following link:

http://www.roseindia.net/xml/dom/CreatXMLFile.shtml

Thanks









Related Tutorials/Questions & Answers:
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
Version of com.stanfy>gson-xml-java dependency
List of Version of com.stanfy>gson-xml-java dependency
Advertisements
Maven Dependency gson-xml-java >> 0.1.4
You should include the dependency code given in this page to add Maven Dependency of com.stanfy >> gson-xml-java version0.1.4 in your project
Maven Dependency gson-xml-java >> 0.1.5
You should include the dependency code given in this page to add Maven Dependency of com.stanfy >> gson-xml-java version0.1.5 in your project
Maven Dependency gson-xml-java >> 0.1.6
You should include the dependency code given in this page to add Maven Dependency of com.stanfy >> gson-xml-java version0.1.6 in your project
Maven Dependency gson-xml-java >> 0.1.7
You should include the dependency code given in this page to add Maven Dependency of com.stanfy >> gson-xml-java version0.1.7 in your project
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
Processing XML with Java
; } Processing XML with Java XML is cross-platform software, hardware... the mechanism to read information from xml document. It was initially Java only... DOM and integrates DOM and SAX. It is tree based java api to work with 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.
Maven Repository/Dependency: com.stanfy | gson-xml-java
Maven Repository/Dependency of Group ID com.stanfy and Artifact ID gson-xml-java. Latest version of com.stanfy:gson-xml-java dependencies. # Version Release Date 1 0.1.7
Maven dependency for com.stanfy - gson-xml-java version 0.1.6 is released. Learn to use gson-xml-java version 0.1.6 in Maven based Java projects
Maven dependency for  com.stanfy  - Version 0.1.6 of gson-xml-java released The developers of   com.stanfy - gson-xml-java project have... of  com.stanfy - gson-xml-java library is 0.1.6. Developer can use
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
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
creating Xml - Java Beginners
creating Xml  how we can create a xml file using java Creating XML File is possible in java with this packages. import... this Elemnts to DOcumnet object. Creating XML File is possible  Creating XML File is possible
XML with JAVA - XML
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
xml in java - Java Beginners
xml in java  how to create a database in xml and then display its content in html table?  Hi Friend, Try the following code: import...); System.out.println("Xml File Created Successfully"); File file = new File("C
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
j2me with xml - Java Beginners
j2me with xml   [email protected] i am working in j2me and i want to read and display data using xml file. In j2me application KXML PARSER is use to read and display data. When i run my application in ?Wireless
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
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for gson-xml-java version 0.1.7
:gson-xml-java:0.1.7 Java library in your project.. Maven, Gradle, SBT, Ivy, Grape, Leiningen and  Buildr Dependency for gson-xml-java version 0.1.7..., Grape, Leiningen and  Buildr Dependency for gson-xml-java version 0.1.7.
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for gson-xml-java version 0.1.6
:gson-xml-java:0.1.6 Java library in your project.. Maven, Gradle, SBT, Ivy, Grape, Leiningen and  Buildr Dependency for gson-xml-java version 0.1.6..., Grape, Leiningen and  Buildr Dependency for gson-xml-java version 0.1.6.
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for gson-xml-java version 0.1.5
:gson-xml-java:0.1.5 Java library in your project.. Maven, Gradle, SBT, Ivy, Grape, Leiningen and  Buildr Dependency for gson-xml-java version 0.1.5..., Grape, Leiningen and  Buildr Dependency for gson-xml-java version 0.1.5.
Maven, Gradle, SBT, Ivy, Grape, Leiningen and Buildr Dependency for gson-xml-java version 0.1.4
:gson-xml-java:0.1.4 Java library in your project.. Maven, Gradle, SBT, Ivy, Grape, Leiningen and  Buildr Dependency for gson-xml-java version 0.1.4..., Grape, Leiningen and  Buildr Dependency for gson-xml-java version 0.1.4.
Converting HTML to XML - Java Beginners
Converting HTML to XML  Hi, I am generating an HTML file (JSP from a Java Struts App.) I need to figure out a way to create an XML file from that HTML document. If you can refer me to some Java Code, that would
Read the value from XML in java
Read the value from XML in java  Hi, i have an XML with the following code. I need to get the path("D:/Lakki/PermissionCheck/logs/RevAppserver.log") of log file in order to check
Java code to parse xml - Java Beginners
Java code to parse xml   TL1 10.50.26.98... xml . after fetching the values i have to use them in the same order i mean like...   Hi, Please read the example at http://www.roseindia.net/xml/dom
Processing XML with Java
Processing XML using Java In this section we will see the API's available in Java for processing the XML. XML file is used on the internet to share... for processing the XML file. The Java XML Processing API provides
XML Books
; Processing XML with Java Welcome to Processing XML with Java, a complete tutorial about writing Java programs that read... integrating XML with Java (and vice versa) you can buy. It contains over 1000 pages
Java Architecture for XML Binding (JAXB)
Java Architecture for XML Binding (JAXB)       XML and Java together are the most ideal building blocks... an XML document through the Java programming language is through SAX or DOM parsers
Convert PDF to XML in Java
Convert PDF to XML File in Java In this Java tutorial section, you will learn how to convert pdf file to xml using java program. We have used itext api... for a transformation result in XML.After that Transformer class process XML from
XML Tutorials
-structured and so easy to grasp that they quickly shift a beginner to XML-Java... Java XML processing APIs. Tutorial starts with the brief introduction to XML... examples to help you master using XML with Java. Advance topics like JAXP
Unmarshalling Jaxb
Unmarshalling Jaxb  How to unmarshal very large xml to java objects usin jaxb/sax parser
XML Tutorials
Re: XML SCHEMA MATCHING doubt's - XML
Re: XML SCHEMA MATCHING doubt's  how to store xml file in database? which database require to store xml file? how to connect xml with java
Re: XML SCHEMA MATCHING doubt's - XML
Re: XML SCHEMA MATCHING doubt's  how to store xml file in database? which database require to store xml file? how to connect xml with java
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
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
What XML JavaObject-XML transformation tools to use? JDOM, Dom4J, XOM, XStream, JAXB, JiBX, PojoXml
to transform java objects to XML and back from XML to Java Objects. Provided the JAVA Objects are predefined and the xml schema/DTD is predefined. What is the best...What XML JavaObject-XML transformation tools to use? JDOM, Dom4J, XOM, XStream
read xml
read xml   hi all, i want to ask about how to read an xml in java ME.. here is the xml file <data> <value> <struct> <member> <name> User_Name
XML, XML Tutorial, XML Tutorial Online, XML Examples, XML Tutorial Example
Processing XML with Java Processing XML with Java DOM JDOM Examples XML DOM...XML XML Tutorials and examples In this section we will learn XML
Getting The XML Root Element
; root element from the XML document.  The JAXP (Java APIs for XML Processing... (file). Both Java and the XML file are kept in the same directory. This program... of XML in Java?": ADS_TO_REPLACE_2 Here is the XML File: Employee
XML Well-Formed-ness
is the video insturction "How to check well formed XML in Java?": ADS...XML Well-Formed-ness      ...; of a XML using the DOM interface. A  well-formed  XML  document must
Create - XML File (Document)
;How to create XML in Java?": ADS_TO_REPLACE_2 Here is Java File... Create - XML File (Document)       In this section, you will  learn to create a XML document
Part I
and so easy to grasp that they quickly shift a beginner to XML-Java programming... various XML Java APIs and Packages ADS_TO_REPLACE_5    JAXP -SAX...Part I. Understanding XML  A1. Understanding XML :  Learn XML
Java Generate XML Example
Java Generate XML Example To Create a XML in Java using Dom follow the Steps...(document); An example for creating the XML in Java is given below package... rootElement.appendChild(ChildElement); 7. Create the output formate // Creating XML output
spring context dependency maven code
configuration metadata from XML, Java annotations, and/or Java code in your program
X - Java Terms
; Java API for XML-based Remote Procedure Calls JAX-RPC stands for Java API for XML-based RPC. The RPC (Remote Procedure Call... to a remote server.     Java Architecture for XML Binding
Exception Handling
the exception handling can be done in java program. This example reads two...\xml>javac exceptionHandle.java C:\vinod\xml>java
Searching an Element in the given XML Document
insturction "How to search element in XML using Java?": ADS_TO_REPLACE_2 Here... Searching an Element in the given XML Document   ... in the specified XML document using DOM APIs defined
JPA Retrieve Data By Using Many-to-Many Relation
(); book1.setBookName("Enterprise Applications Integration with XML and Java... Integration with XML and Java Hibernate: insert into author_book

Ads