Share on Google+Share on Google+

divya
how to read values from java in xml?
1 Answer(s)      6 years and a month ago
Posted in : WebSevices

how to read values from java in xml?

Ads

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:
Advertisements
Tutorials   
Java Spring Hibernate Struts Training 6 SaaS Software Solutions Every Business Owner Should Consider WebServices Study Material What is the use of MySQL, MS SQL in Web Hosting paradigm? What do you understand by Domain Parking? What is difference between UNIX Hosting and Window Hosting? Expalin the Web Hosting Bandwidth/Space/Transfer What do you understand by Virtual Hosting? How to Upload Site Online? How to register domain name for my Company? How to track how many hits my website gets? create plugins to detect suspicious ajax chrome extension what timing attack {practically}??? any one can help me in my Ph.d thesis Accessing your site How to develope web service with exachanging xml message between computers using SOAP How to Convert SOAP Web service to REST Web service In Java ? add a new wcf service code to TFS and make it available to create an MSI. of its How we can integrate ASP .net website to payment gateway using SOAP xml request and response using wsdl. input fields to reject all inconsistent characters in webconfig Search operation in JSP page Where can I find reviews of IT solutions company in India to choose from? help regarding creation of last year project Select Box question Web Services Tutorials What is SOA and Webservices? PHP webdesign Pinting restful webservice request on UI. asp.net MaxClients apache httpd.conf max connections Large File reading through Axis2 Web service reg tutorial http://www.roseindia.net/webservices/netbeans/JAX-RPCAdvanceConcepts.shtml What is Web Development? Get GMT from IP WEB SERVICES Create a Web Service in Java using Apache Axis2 and Eclipse with Oracle database driven local host sharing on same network compute Internet and Web related questions for Web Master. web technology What is webservices? wifi problem wifi problem Overriding jaxb binding at client side? How to connect one application to some othe application through Rest json Database with dotnet while playing youtube in iphone appliation, coming black screen in iphone project? programming questions Web Analytics Java Runtime Exception: 0x4F4F:524A 0x4120:5445 0x4348:0D0A Restful webservice

Ads

 
Advertisement null

Ads