how to read values from java in xml?

how to read values from java in xml?

how to read values from java in xml?

View Answers

March 5, 2011 at 10:48 AM

Java read values from console and stored it into xml file

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

public class CreatXMLFile {
  public static void main(String[] args) throws Exception {
    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter number to add elements in your XML file: ");
    String str = bf.readLine();
    int no = Integer.parseInt(str);
    System.out.print("Enter root: ");
    String root = bf.readLine();
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder =documentBuilderFactory.newDocumentBuilder();
    Document document = documentBuilder.newDocument();
    Element rootElement = document.createElement(root);
    document.appendChild(rootElement);
    for (int i = 1; i <= no; i++){
      System.out.print("Enter the element: ");
      String element = bf.readLine();
      System.out.print("Enter the data: ");
      String data = bf.readLine();
      Element em = document.createElement(element);
      em.appendChild(document.createTextNode(data));
      rootElement.appendChild(em);
    }
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result =  new StreamResult(new File("C:/file.xml"));
        transformer.transform(source, result);
  }
}









Related Tutorials/Questions & Answers:
how to read values from java in xml?
how to read values from excel sheet and compare with database using jsp
Advertisements
How to values from xml using java?
XMLA
How to read value from xml using java?
How to read text from - Java Beginners
Read from file java
ModuleNotFoundError: No module named 'xmlr'
ModuleNotFoundError: No module named 'xmlx'
how to read from dictionary c
how to get the values from dynamically generated textbox in java?
How to read from stdin in python
How to return multiple values from a method - Java Beginners
How to return multiple values from a method - Java Beginners
How to update table column from the values of Arraylist in java
How can i read a file from nth line onwards in java
How to read PDF files created from Java thru VB Script
How to read bytes from a Linked list - Java Beginners
How to get Keys and Values from HashMap in Java?
how to insert values from jsp into ms access
Read from a window - Java Beginners
Java read lines from file
how to get a values from processRecord - JSP-Servlet
How to pass multiple values from a servlet to JSP?
Read the value from XML in java
ModuleNotFoundError: No module named 'xmla'
ModuleNotFoundError: No module named 'xmla'
how to fetch values from .properties to a html file
How to carry multiple values from a Servlet to a JSP?
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...
How to read all value from int buffer in java.
Read Bit values
How to create file from input values in Jframe ?
how to read the .proprties file from struts - Struts
How to read a line from a doc file
How to Display values from databse into table
how to get the values to dropdownlist from oracle database
how to store multiple values from drop down in database where i am using java struts 1.3
How to read and display data from a .properties file from a jsp page
read excel file from Java - Java Beginners
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file
how to pass the mutiple values from <Ui:datagrid hyperlink - JSP-Servlet
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database? - XML
How to read and display password from the console
how to read 100 text files from a folder or directory and write the data into a single file.using java programming?
How to read loops in Java
how to count unique and duplicate values from one table in mysql?
how to set values from parent page to iframe in javascript
How to read data from txt file and save to database MySql - Java Beginners

Ads