Source Code : Use JDOM to build a document
Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com.
Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com.
Click Here or search from google with Libraries Name you get jar file related it
Use JDOM to build a document
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.util.*;
import java.io.*;
public class JDOMTest {
public static void showBooks( Element root ) {
List books = root.getChildren( "book" );
for( Iterator i=books.iterator(); i.hasNext(); ) {
Element book = ( Element )i.next();
System.out.println( "Book: " + book.getAttributeValue( "category" ) + ", " +
book.getChildTextTrim( "title" ) + ", " +
book.getChildTextTrim( "author" ) + ", " +
book.getChildTextTrim( "price" ) );
}
}
public static void main( String[] args ) throws Exception{
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build( "book.xml" );
Element root = doc.getRootElement();
System.out.println( "Book List Before: " );
showBooks( root );
// Add a new book
Element newBook = new Element( "book" );
newBook.setAttribute( "category", "fiction" );
Element newTitle = new Element( "title" );
newTitle.addContent( "Desecration" );
Element newAuthor = new Element( "author" );
newAuthor.addContent( "Tim LaHaye" );
Element newPrice = new Element( "price" );
newPrice.addContent( "19.95" );
newBook.addContent( newTitle );
newBook.addContent( newAuthor );
newBook.addContent( newPrice );
root.addContent( newBook );
System.out.println( "Book List After: " );
showBooks( root );
XMLOutputter out = new XMLOutputter( " ", true );
out.output( root, System.out );
}
}
<books>
<book category="fiction">
<title>title 1</title>
<author>Tim Lahaye</author>
<price>14.95</price>
</book>
<book category="science fiction">
<title>title 2</title>
<author>Tim Lahaye</author>
<price>14.95</price>
</book>
</books>
Thank with us