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);
  }
}

Ads









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?
How to read value from xml using java?
how to get the values from dynamically generated textbox in java?
Read from file java
how to read from dictionary c
How can i read a file from nth line onwards in java
How to update table column from the values of Arraylist in java
How to read from stdin in python
How to read PDF files created from Java thru VB Script
How to get Keys and Values from HashMap in Java?
how to insert values from jsp into ms access
How to pass multiple values from a servlet to JSP?
How to carry multiple values from a Servlet to a JSP?
how to fetch values from .properties to a html file
Java read lines from file
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...
How to create file from input values in Jframe ?
Read the value from XML in java
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
Read Bit values
How to read loops in Java
How To Read Integer From Command Line In Java
How to read and display data from a .properties file from a jsp page
ModuleNotFoundError: No module named 'xmlr'
ModuleNotFoundError: No module named 'xmlx'
how to read 100 text files from a folder or directory and write the data into a single file.using java programming?
how to retreive values from MS Access Database based on the values entered in textbox values in jsp file

Ads