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

Tags: , , , , ,

Sponsored Links



How to create an XML document using JDOM?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 29-01-2010
Member
 
Join Date: Nov 2009
Posts: 131
How to create an XML document using JDOM?

Hello to all,
I am new to this forum. I am last year Computer science student. I am working on live project where I am using Java as front end and SQl as back end. I want to create an XML document using JDOM? I tried various method but none of them worked out. Can anyone tell me how to create an XML document using JDOM?
Thanks in advanced.

Last edited by Juany : 29-01-2010 at 03:37 PM.
Reply With Quote
  #2  
Old 29-01-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
Re: How to create an XML document using JDOM?

From the following program you will see how to use JDOM to create a simple xml file. You also able to see how to create elements of the xml document and hopw ro set some text for the element.

After this you will bale to see how to use the XMLOutputter class to write the JDOM document into file and display it on the screen. To make the output better we have to apply a Format to our xml document.


Code:
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
import org.jdom.output.Format;

import java.io.FileWriter;
import java.io.IOException;

public class JDomtoXml {
    public static void main(String[] args) {
       

        Document documents = new Document();
        Element roots = new Element("rows");

        
        Element childs = new Element("row");
        child.addContent(new Element("first").setText("capsul"));
        child.addContent(new Element("last").setText("karan"));
        child.addContent(new Element("address").setText("majas road"));

        
        root.addContent(childs);
        document.setContent(roots);
        
        try {
            FileWriter writers = new FileWriter("clientinfo.xml");
            XMLOutputter outputters = new XMLOutputter();

            
            outputter.setFormat(Format.getPrettyFormat());
            
            
            outputter.output(documents, writers);
            outputter.output(documents, System.out);
        } catch (IOException es) {
            es.printStackTrace();
        }
    }
}
Reply With Quote
  #3  
Old 29-01-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
Re: How to create an XML document using JDOM?

It is possible to make up and write an XML document using JDOM. It is very easy process. In the following program I have use DoctoJDOM class. In that I have create two integer dws and docs. I also made two column box to give description about 2 text box.




Code:
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class DoctoJDOM {

  public static void main(String[] av) throws Exception {
    DoctoJDOM dws = new DocsWriteJDOM();
    Document docs = dw.makeDocs();
    
    new XMLOutputtesr().output(docs, System.out);
  }


  protected Document makeDocs() throws Exception {
      Document docs = new Document(new Element("kavita"));
      docs.getRootElement().
        addContent(new Element("romania").
          addContent(new Element("kiresh").
              setText("Oonce upon time")).
          addContent(new Element("kind").
              setText("I love my country ")));

      return docs;
  }
}
Reply With Quote
  #4  
Old 29-01-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
Re: How to create an XML document using JDOM?

You must know how to construct a JDOM Document from an external source like following code.

Code:
SAXBuilder builders = new SAXBuilder();
Document docs = builders.build(url);
It is also possible to construct a JDOM Document with no existing XML data store.

Code:
Element roots = new Element("rootelements");
Document docs = new Document(roots);
You can also construct an Element and a Document using above Element as the document root. Normal document manipulation can occur in following code.

Code:
root.setText("this is right element ");
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 29-01-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
Re: How to create an XML document using JDOM?

You have to use following step to create an XML document using JDOM.
Just open text editor and save following code in that.

Code:
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
Step 2
Now start with new Java class DoctoJDOM with new XML Documents using the JDOM libraries.

Code:
public class DoctoJDOM { }
Step 3

Now Define JDOMDoc() method that will create a new XML document using the JDOM library. You can do this by typing the following code after the { bracket in the class definition.

Code:
public Document JDOMDoc() throws Exception
{
Document JDOMDocs = new Document(new Element("kyuntry"));
myJDOMDoc.getRootElement().
addContent(new Element("Wlouinyt").
addContent(new Element("sdsvzc").
setText("bvgdbd")).
addContent(new Element("xbdfdd").
setText("dfbd")));

return JDOMDocs;
}
Reply With Quote
  #6  
Old 29-01-2010
Modifier's Avatar
Member
 
Join Date: Jan 2008
Posts: 1,502
Re: How to create an XML document using JDOM?

You have to use following code to create an XML document using JDOM. Just try to use following code. It is very easy process.



Code:
import org.jdom.Document;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class OuttoXmlJdom {
  public static void main( String[] args ) {
    try {
     
      SAXBuilder builders = new SAXBuilder();
      builders.setExpandEntities( true );
      Document docs = builders.build( "drainpipes.xml" );

    
      XMLOutputter xmlOutputters = new XMLOutputter();
      xmlOutputters.setTextTrim( true );
      xmlOutputters.setIndent( 2 );
      xmlOutputters.setNewlines( true );

    
      xmlOutputters.output( docs, System.out );
    }
    catch( Exception ioes ) {
      ioes.printStackTrace();
    }
  }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to create an XML document using JDOM?"
Thread Thread Starter Forum Replies Last Post
Help with JDOM andyfer Software Development 1 30-04-2011 02:37 AM
Indesign - create interactive document Bricklayer Windows Software 4 14-07-2010 01:55 PM
How to Create Documents Online Via Box.net Web Document Snake08 Tips & Tweaks 0 26-02-2009 06:26 PM
create a document using Word Moiz Windows Software 3 02-02-2009 05:35 PM
UNABLE TO CREATE A DOM DOCUMENT John Windows XP Support 5 28-01-2007 09:51 PM


All times are GMT +5.5. The time now is 11:16 AM.