Results 1 to 6 of 6

Thread: How to create an XML document using JDOM?

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

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

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

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    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;
      }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    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 ");

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    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;
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

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

Similar Threads

  1. Help with JDOM
    By andyfer in forum Software Development
    Replies: 1
    Last Post: 30-04-2011, 01:37 AM
  2. Indesign - create interactive document
    By Bricklayer in forum Windows Software
    Replies: 4
    Last Post: 14-07-2010, 12:55 PM
  3. Replies: 0
    Last Post: 26-02-2009, 06:26 PM
  4. create a document using Word
    By Moiz in forum Windows Software
    Replies: 3
    Last Post: 02-02-2009, 05:35 PM
  5. UNABLE TO CREATE A DOM DOCUMENT
    By Kana in forum Windows XP Support
    Replies: 4
    Last Post: 28-01-2007, 09:51 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,713,445,994.78237 seconds with 16 queries