Results 1 to 4 of 4

Thread: How to create XML file in Java

  1. #1
    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...

  2. #2
    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);
      }
    }

  3. #3
    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.

  4. #4
    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();
        }
      }
    }

Similar Threads

  1. Replies: 1
    Last Post: 21-03-2013, 03:52 PM
  2. Windows 7: Unable to create file with File system error (65535)
    By TheHibiscus in forum Operating Systems
    Replies: 4
    Last Post: 23-01-2011, 07:07 PM
  3. Create Zip File in Java
    By Kushan in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 01:04 PM
  4. Can I create .exe file in java?
    By LostIt in forum Software Development
    Replies: 3
    Last Post: 08-10-2009, 06:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,715,763,319.15608 seconds with 17 queries