Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java create xml

Create a DocumentBuilder instance.
Create a Document from the above DocumentBuilder .
Create the elements you want using the Element class and its appendChild method.
Create a new Transformer instance and a new DOMSource instance.
Create a new StreamResult to the output stream you want to use
Comment

How to create XML file in Java

package com.example.TestApp;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CreateXMLFileJava {

 public static void main(String[] args) throws ParserConfigurationException, 
                                               IOException, 
                                               TransformerException
    {
     // 1.Create a DocumentBuilder instance
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dbuilder = dbFactory.newDocumentBuilder();

    // 2. Create a Document from the above DocumentBuilder.
    Document document = dbuilder.newDocument();

    // 3. Create the elements you want using the Element class and its appendChild method.   

   // root element
    Element users = document.createElement("users");
    document.appendChild(users);   

    // child element
    Element user = document.createElement("user");
    users.appendChild(user);  

    // Attribute of child element
    user.setAttribute("uid", "1");   

    // firstname Element
    Element firstName = document.createElement("firstName");
    firstName.appendChild(document.createTextNode("Interview"));
    user.appendChild(firstName);   

    // lastName element
    Element lastName = document.createElement("lastName");
    lastName.appendChild(document.createTextNode("Bubble"));
    user.appendChild(lastName);   

    // email element
    Element email = document.createElement("email");
    email.appendChild(document.createTextNode("admin@interviewBubble.com"));
    user.appendChild(email);   

    // write content into xml file   

    // 4. Create a new Transformer instance and a new DOMSource instance.
   TransformerFactory transformerFactory = TransformerFactory.newInstance();
   Transformer transformer = transformerFactory.newTransformer();
   DOMSource source = new DOMSource(document);

   // 5. Create a new StreamResult to the output stream you want to use.
   StreamResult result = new StreamResult(new File("/Users/admin/Desktop/users.xml"));
   // StreamResult result = new StreamResult(System.out); // to print on console

   // 6. Use transform method to write the DOM object to the output stream.
   transformer.transform(source, result);  

   System.out.println("File created successfully");
 }
}
Comment

java create xml


//Root Element
Element root=new Element("CONFIGURATION");
Document doc=new Document();
//Element 1
Element child1=new Element("BROWSER");
//Element 1 Content
child1.addContent("chrome");
//Element 2
Element child2=new Element("BASE");
//Element 2 Content
child2.addContent("http:fut");
//Element 3
Element child3=new Element("EMPLOYEE");
//Element 3 --> In this case this element has another element with Content
child3.addContent(new Element("EMP_NAME").addContent("Anhorn, Irene"));

//Add it in the root Element
root.addContent(child1);
root.addContent(child2);
root.addContent(child3);
//Define root element like root
doc.setRootElement(root);
//Create the XML
XMLOutputter outter=new XMLOutputter();
outter.setFormat(Format.getPrettyFormat());
outter.output(doc, new FileWriter(new File("myxml.xml")));

Comment

PREVIOUS NEXT
Code Example
Java :: Caused by: java.io.EOFException: SSL peer shut down incorrectly 
Java :: java instantiate a scanner 
Java :: debug in java 
Java :: class, interface, or enum expected java 
Java :: findone in spring boot 2.4.1 
Java :: regex double quote java 
Java :: how to read text file as variable in java 
Java :: is it possible to quick sort a string in java 
Java :: java stringbuilder set value 
Java :: convert object array to int array java 
Java :: binary to decimal java 
Java :: power-hungry foobar solution in java 
Java :: sorting collections with comparator java 
Java :: how to disable screenshot in react native 
Java :: java get element occurrences in a list java 
Java :: initilize an array java 
Java :: how to do annotation configuration in spring 
Java :: java static variable 
Java :: Java Searching Using binarySearch() 
Java :: java.lang.ClassNotFoundException: javax.servlet.jsp.jstl.core.Config 
Java :: deifine an integer array in java 
Java :: mvn spring boot ends when closing vm 
Java :: default java keystore 
Java :: count true in matrix java 
Java :: horizontal recyclerview item width half of screen android 
Java :: how to store date in java 
Java :: how to translate java swing 
Java :: java sub method 
Java :: java program to convert milliseconds to minutes and seconds 
Java :: Read json url in android - NAYCode.com 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =