MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology

JAVA XML

JavaXml Projects

JavaXml Project 1

JavaXml Examples

JavaXml EXAMPLE

Create XML Document
Previous Home Next

With help of this program, we will be learn to create a XML document using the DOM Application programming interfaces. In This XML document uses 1.0 version and UTF-8 encoding. By help of this program we can create a XML document on the console. This program asks for the number of elements to be added in the generated xml file. It takes the root name at the console and passes it in the createElement() method. It creates the Element object and invokes the Document object . Depending upon the given number, it creates that much elements and fills them with data. Finally, it displays the generated XML file with its version and encoding.

Example

Here is Java File: Studentrecord.java

 

package r4r;
import org.w3c.dom.*;
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
[an error occurred while processing this directive]
public class createxml {
	public static void main(String[] args) throws Exception {
		  BufferedReader br = new BufferedReader(
		  new InputStreamReader(System.in));
		System.out.print("Enter number to add elements in our XML file: ");
		  String str = br.readLine();
		  int nu = Integer.parseInt(str);
		  System.out.print("Enter root: ");
		  String root = br.readLine();
		  DocumentBuilderFactory docFact = DocumentBuilderFactory.newInstance();
		  DocumentBuilder docBuild = docFact.newDocumentBuilder();
		  Document docu = docBuild.newDocument();
		  Element rEle = docu.createElement(root);
		  docu.appendChild(rEle);
		  for (int i = 1; i <= nu; i++){
		  System.out.print("Enter the element: ");
		  String element = br.readLine();
		  System.out.print("Enter the data: ");
		  String data = br.readLine();
		  Element em = docu.createElement(element);
		  em.appendChild(docu.createTextNode(data));
		  rEle.appendChild(em);
		  }
		  TransformerFactory transFact = TransformerFactory.newInstance();
		  Transformer trans = transFact.newTransformer();
		  DOMSource dsrc = new DOMSource(docu);
		  StreamResult result =  new StreamResult(System.out);
		  trans.transform(dsrc, result);
		  }
}
Previous Home Next