|
| |||||||||
| Tags: application, class, document, java, jdom, object |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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
| ||||
| ||||
| 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
| ||||
| ||||
| 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;
}
}
__________________ Grand Theft Auto 4 PC Video Game |
|
#4
| ||||
| ||||
| 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); Code: Element roots = new Element("rootelements");
Document docs = new Document(roots); Code: root.setText("this is right element ");
__________________ The FIFA Manager 2009 PC Game |
|
#5
| ||||
| ||||
| 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; Now start with new Java class DoctoJDOM with new XML Documents using the JDOM libraries. Code: public class DoctoJDOM { } 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
| ||||
| ||||
| 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();
}
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |