You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by "Vikas Jolly(Yahoo)" <vi...@yahoo.com> on 2001/04/19 10:58:43 UTC

Create Element using DOM in JAVA

Hi ALL

I am new to XML and i am trying to create a simple element using the DOM.
My problem is that no data is stored in the xml file.

Following is my code. 

import java.io.*;
import org.w3c.dom.*;
import com.ibm.xml.parsers.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;

/**
 * A sample DOM writer. This sample program illustrates how to
 * create an xml document using DOM 
 * @author Vikas Jolly
 */

public class WriteXMLWithDOM {

  /** Main program entry point. */
  public static void main(String argv[])  {
    
    if (argv.length == 0)  {
      System.out.println("Usage:  java WriteXMLWithDOM MyFile.xml");
      System.exit(1);
    }

    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance ();
     DocumentBuilder db = dbf.newDocumentBuilder ();
     
     Document doc = db.newDocument ();
     System.out.println("doc loaded.............");
        
        Element root = doc.createElement("sonnet");
        System.out.println("Element Created...root = "+root);
        
        Node n = doc.appendChild(root);
        System.out.println("Element append...n = "+n);

    }catch(Exception e) {
        System.out.println("e = "+e);
    }
    
  }
}

I run this program from command  line as :

java java WriteXMLWithDOM MyFile.xml

Note : Here MyFile.xml is an empty xml file.


Kindly reply.

vikas



_________________________________________________________
Do You Yahoo!?
Get your free @yahoo.com address at http://mail.yahoo.com


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org