XML from Java-classes


There is a set of the reasons to consider{examine} Ajax-searches in performance XML: each browser supporting Ajax, has methods of navigation under XML-documents and there are many technologies for job on the server with the XML-g.iven it is very easy to understand mutual relation between your Ajax-client and the server, using the circuit of the description of types of documents which vary and if you apply the approach to architecture of the server, suitable for services, use XML will allow the clients who are not supporting Ajax, to realize your data.


I rassmotrju three ways which you can use for manufacture of the XML-given from Java-objects and rassmotrju merits and demerits of everyone.



Start own serialization


First of all, you could generate XML directly from yours the column of objects. This approach can be simple also, as well as toXml () a method in each of your JavaBean-classes. Then you, probably, will pick up suitable interface XML API and will make for everyone issued bean an element performance and will recurrently cause components the column of object. Clearly, that such method will not cope with the big number of classes as each class will need to write own creating XML a code. On the other hand it enough the simple method for realization in which there is no overload because of an additional configuration or more and more becoming complicated process of construction, and is not required other circuit made of yours JavaBeans which can be transformed into the XML-document with the help of pair calls.


In a code of an example of previous clause{article} of this series, I realized toXml () methods, having added lines XML. As I already have mentioned earlier, it is the limited method as he enters a volumetric code for acknowledgement{confirmation} zadumyvaemykh the links, ciphered data and so on for each method toXml (). Some interfaces API XML are accessible on a Java-platform to performance of all this job for you, allowing you to concentrate on content XML. Listing 1 uses for realization toXml () in a class, were turn in an example of a database (see figure 1).


Listing 1. JDOM realization toXml () for class Order



public Element toXml () {


Element elOrder = new Element ("order");

elOrder.setAttribute ("id", id);


elOrder.setAttribute ("cost", getFormattedCost ());


Element elDate = new Element ("date") .addContent (date);

elOrder.addContent (elDate);


Element elItems = new Element ("items");

for (Iterator <Item> iter =

items.iterator (); iter.hasNext ();) {

elItems.addContent (iter.next () .toXml ());

} elOrder.addContent (elItems);


return elOrder;

}


Now you see how simply to create elements, to establish attributes and to add contents of an element with help JDOM. Recursive calls for drawing up JavaBeans toXml () methods are intended for Element performances of their subschemes. For example, contents of an element items are generated here with the help of a call toXml () for each object Item, the counted up Order - Order.


When all you JavaBeans will execute a method toXml (), it will be easy serializovat` any any circuit of objects in XML the document and to return to his{its} Ajax-client, as shown in listing 2.


Listing 2. Generating of XML-search from a JDOM-element



public void doGet (HttpServletRequest req, HttpServletResponse res)

throws java.io. IOException, ServletException {


String custId = req.getParameter ("username");

Customer customer = getCustomer (custId);


Element responseElem = customer.toXml ();

Document responseDoc = new Document (responseElem);


res.setContentType ("application/xml");

new XMLOutputter () .output (responseDoc, res.getWriter ());

}


JDOM again will simplify to you a life. Only it is necessary for you to create for a XML-element returned by the circuit of object, Document and then to use XMLOutputter for copying servleta search. Listing 3 sets an example XML, executed in such a way, formatted by initialization XMLOutputter with help Format.getPrettyFormat () JDOM. In this case the buyer has made the unique order consisting of two things.


Listing 3. The example of the XML-document representing the buyer



<? xml version = " 1.0" encoding = "UTF-8"?>

<customer username = "jimmy66">

<realname> James Hyrax </realname>

<orders>

<order id = "o-11123" cost = " $ 349.98">

<date> 08-26-2005 </date>

<items>

<item id = "i-55768">

<name> Oolong 512MB CF Card </name>

<description> 512 Megabyte Type 1 CompactFlash card.

Manufactured by Oolong Industries </description>

<price> $49.99 </price>

</item>

<item id = "i-74491">

<name> Fujak Superpix72 Camera </name>

<description> 7.2 Megapixel digital camera featuring six

shooting modes and 3x optical zoom. Silver. </description>

<price> $299.99 </price>

</item>

</items>

</order>

</orders>

</customer>