Using Java to generate XML

With Java you can generate XML in many ways. One of the ways is to write the XML code using the out.println() statement and generate the XML file you need using the PrintWriter. However this method is very straightforward and there is no guarantee that the generated XML file would be error free.


_______________________________________________

Another way is to use the Java API for XML Processing. By using this method you can create a document object and then create the different elements of the XML file using the Element object. The document object has methods for this purpose.

Methods like appendChild() and createTextNode() are used in creating the text nodes and then adding them to the root node of the XML document.

One another way to generate XML from Java is to serialize the Java objects in to XML using the XStream class. With this method you can easily serialized the Java objects to XML. For example if you have a class 'person', you can serialize that with the statement,

String xml = xs.toXML(person);

In the above statement 'xs' is the XStream object. To work with this object you have to import com.thoughtworks.xstream.XStream.




_______________________________________________

FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

_______________________________________






 

 

FREE Subscription

Stay Current With the Latest Technology Developments Realted to XML. Signup for Our Newsletter and Receive New Articles Through Email.

Name:

Email:

Note : We never rent, trade, or sell our email lists to anyone. We assure that your privacy is respected and protected.

 

 

Add to My Yahoo!

Visit XML Training Material Guide Homepage

 

 

Copyright - © 2004 - 2017 - All Rights Reserved.