how to read values from java in xml?

Ads
 

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 read value from xml using java?
How to values from xml using java?
How to read text from - Java Beginners
XMLA
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 from dictionary c
How to read bytes from a Linked list - Java Beginners
Read from file java
how to get the values from dynamically generated textbox in java?
How to return multiple values from a method - Java Beginners
How to return multiple values from a method - Java Beginners
How to read loops in Java
How to update table column from the values of Arraylist in java
How to read all value from int buffer in java.
Read from a window - Java Beginners
Java read lines from file
How to get Keys and Values from HashMap in Java?
how to get a values from processRecord - JSP-Servlet
read excel file from Java - Java Beginners
how to read the .proprties file from struts - Struts
how to insert values from jsp into ms access
How to read and retrieve jtable row values into jtextfield on clicking at particular row ...
how to read 100 text files from a folder or directory and write the data into a single file.using java programming?
Read the value from XML in java
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
how to store multiple values from drop down in database where i am using java struts 1.3

Ads