Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 27-04-2009
Member
 
Join Date: Apr 2009
Posts: 68
How to create XML file in Java

Hello Friends,

I am New to the Programming world, and i have started working on JAVA, i have 2 to 3 months experience on it, and am looking forward to create an XML file in Java, so please let me know, how to create XML file in Java.

Thanks in Advance...
Reply With Quote
  #2  
Old 27-04-2009
Member
 
Join Date: Mar 2008
Posts: 258
Re: How to create XML file in Java

This program helps in creating a XML document on the console. This program asks for the number of elements to be added in the generated xml file. It takes the root name at the console and passes it in the createElement() method. It creates the Element object and invokes the Document object . Depending upon the given number, it creates that much elements and fills them with data,. Finally, it displays the generated XML file with its version and encoding.

Here is Java File: CreatXMLFile.java

Code:
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(System.out);
        transformer.transform(source, result);
  }
}
Reply With Quote
  #3  
Old 27-04-2009
Member
 
Join Date: Apr 2009
Posts: 68
Re: How to create XML file in Java

Thanks Khushal this really work out, this is what i wanted to have to create the XML file.

If anyone has different programming logic please let me know.

Thanks once again.
Reply With Quote
  #4  
Old 27-04-2009
Member
 
Join Date: Oct 2008
Posts: 167
Re: How to create XML file in Java

Here i will also provide you one more java programming example just have a look at it.

Code:
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

/**
 * A sample application which shows how to perform a
 * XML document validation.
 */

public class Test {
  public static void main(String[] args) {
    try {
      // define the type of schema - we use W3C:
      String schemaLang = "http://www.w3.org/2001/XMLSchema";

      // get validation driver:
      SchemaFactory factory = SchemaFactory.newInstance(schemaLang);

      // create schema by reading it from an XSD file:
      Schema schema = factory.newSchema(new StreamSource("sample.xsd"));
      Validator validator = schema.newValidator();

      // at last perform validation:
      validator.validate(new StreamSource("sample.xml"));

    }catch (SAXException ex) {
      // we are here if the document is not valid:
      // ... process validation error...
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to create XML file in Java"
Thread Thread Starter Forum Replies Last Post
Driver error: Cannot create a file when that file already exists Nickason Windows XP Support 1 21-03-2013 03:52 PM
Windows 7: Unable to create file with File system error (65535) TheHibiscus Operating Systems 4 23-01-2011 07:07 PM
Create Zip File in Java Kushan Software Development 3 08-12-2009 01:04 PM
Can I create .exe file in java? LostIt Software Development 3 08-10-2009 06:23 PM


All times are GMT +5.5. The time now is 01:04 AM.