Create .xml file from a java file
I wanted to know how can we create a.xml file through java with the given data.
For example: book saler.
data given through java are
1)Sl no
2)Book name
3Book type
4)Book price
i want a xml file generated through the java code
can anyone help me out?
[313 byte] By [
querrya] at [2007-10-2 5:50:10]

The below code is written using dom4j in java.. You can look at some other examples in dom4j..
But, here is the short version..
Document document = DocumentHelper.createDocument();
Element root = document.addElement( "BookSeller")
QName qNameReqId = new QName("SerialNo","SlNo");
QName qBookName = new QName("BookName","Book Name");
QName qBookType= new QName("BookType","Book Type");
QName qBookPrice= new QName("BookPrice","Book Proce");
Element reqIdElement = root.addElement( qNameReqId ).addText( 1 );
Element bookNmElement = root.addElement( qBookName ).addText( "Java/XML");
Element bookTypeElement = root.addElement( qBookType).addText( "LEARNING");
Element bookPriceElement = wtnElement.addElement(qBookPrice).addText( "100$");
StringWriter stringWriter= new StringWriter();
XMLWriter xmlWriter = new XMLWriter(stringWriter);
xmlWriter.write(document);
xmlString=stringWriter.toString();
I think something like this could work for you.
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.ContentHandler;
public void writeSAX(ContentHandler ch) throws SAXException {
String name = "Some book name";
String type = "Some book type";
String price ="$10.00";
AttributesImpl attrs = new AttributesImpl();
ch.startElement("","Book type","", attrs);
attrs = new AttributesImpl();
char[] chars = type.toCharArray();
ch.characters(chars,0,chars.length);
ch.startElement("","Book Name","", attrs);
char[] chars = name.toCharArray();
ch.characters(chars,0,chars.length);
ch.endElement("","Book name","");
ch.startElement("","Book Price","", attrs);
char[] chars = price.toCharArray();
ch.characters(chars,0,chars.length);
ch.endElement("","Book Price","");
ch.endlement("", "Book Type","");
}
i have done like this..
but the problem with this is i want to add more than one book details. i am able to add only one book detais..
package name:createxml
main class:GenerateOrderXml.java
CreateXml.java and order.java
/***********************************************************************/
package createxml;
import java.util.Vector;
public class GenerateOrderXml {
public static void main(String[] args) {
try{
Order order= new Order();
CreateXml xml=new CreateXml();
Vector orders=new Vector();
order.setBookName("Java_for_beginers");
order.setAuthorName("Balaguruswamy");
order.setISBN("a123");
order.setNo_of_copies("3");
order.setprice("350");
order.setPublisher("Deepak publishers");
orders.add(0,order);
order.setBookName("Java");
order.setAuthorName("Bala");
order.setISBN("a123");
order.setNo_of_copies("3");
order.setprice("350");
order.setPublisher("Deepak ");
orders.add(1,order);
xml.creatxml(orders);
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
}
}
/************************************************************************/
package createxml;
import org.jdom.*;
import org.jdom.output.XMLOutputter;
import java.io.IOException;
import java.util.Vector;
/**
* @author divyashree
*
* To change the template for this generated type comment go to
* Window - Preferences - Java - Code Generation - Code and Comments
*/
public class CreateXml {
public void creatxml(Vector orders)
{
try
{
Element root = new Element("BookSaler");
Order order= new Order();
order=(Order)orders.elementAt(0);
Element BName = new Element("Bookname1");
BName.setText(order.getBookName());
root.addContent(BName);
Element AName = new Element("Authorname");
AName.setText(order.getAuthorName());
BName.addContent(AName);
Element isbn = new Element("ISBN");
isbn.setText(order.getISBN());
BName.addContent(isbn);
Element price = new Element("Price");
price.setText(order.getprice());
BName.addContent(price);
Element publisher= new Element("Publisher");
publisher.setText(order.getPublisher());
BName.addContent(publisher);
order=(Order)orders.elementAt(1);
Element BName1 = new Element("Bookname2");
BName1.setText(order.getBookName());
root.addContent(BName1);
Element AName1 = new Element("Authorname");
BName1.addContent(AName);
Element isbn1 = new Element("ISBN");
isbn.setText(order.getISBN());
Element price1 = new Element("Price");
BName1.addContent(price);
Element publisher1= new Element("Publisher");
BName1.addContent(publisher);
Document doc = new Document(root);
try {
XMLOutputter serializer = new XMLOutputter();
serializer.setIndent(" ");
serializer.setNewlines(true);
serializer.output(doc, System.out);
}
catch (IOException e)
{
System.err.println(e);
}
}catch(ClassCastException e)
{
e.printStackTrace();
}
}
}
/******************************************************************/
package createxml;
import java.util.Vector;
public class Order {
protected String ISBN="";
protected String BookName="";
protected String AuthorName="";
protected String No_of_copies;
protected String price;
protected String Publisher="";
public String getISBN()
{
return this.ISBN;
}
public String getBookName()
{
return BookName;
}
public String getAuthorName()
{
return AuthorName;
}
public String getNo_of_copies()
{
return No_of_copies;
}
public String getprice()
{
return price;
}
public String getPublisher()
{
return Publisher;
}
public void setISBN(String isbn)
{
Vector vec_isbn= new Vector();
ISBN=isbn;
}
public void setBookName(String bookname)
{
BookName=bookname;
}
public void setAuthorName(String authorname)
{
AuthorName=authorname;
}
public void setNo_of_copies(String no_of_copies)
{
No_of_copies=no_of_copies;
}
public void setprice(String Price)
{
price=Price;
}
public void setPublisher(String publisher)
{
Publisher=publisher;
}
}
/***************************************************************************/