In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to implement Java object serialization". Many people will encounter such a dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Generate XML from the Java class
There are many reasons for passing Ajax responses as XML: every browser that supports Ajax has a way to navigate XML documents, and there are many server-side technologies that can handle XML data.
By developing a scheme that describes the types of documents to be exchanged, it is easy to define contracts between the Ajax client and server side, and if the server-side architecture is service-oriented, then using XML can also allow non-Ajax clients to use the data you provide.
I'll consider three ways to generate XML data from Java objects and discuss the pros and cons of each.
Serialize on your own
First, you can generate XML programmatically from the object graph. This can be as simple as implementing the toXml () method in each JavaBean class. You can then select the appropriate XML API, have each bean provide elements that represent its own state, and recursively invoke the object graph on its own members.
Obviously, this approach cannot be extended to a large number of classes, because each class needs to write its own XML-generated code. On the plus side, this is a simple way to implement, with no additional configuration overhead or more complex build process overhead, and any JavaBean diagram can be turned into an XML document in just a few calls.
I used to concatenate XML tag strings together to implement the toXml () method. As I mentioned last time, this is a bad approach because it places the burden of ensuring tag matching, entity coding, and so on in the code of each toXml () method.
There are several XML API on the Java platform that can do this for you, so you can focus on the content of XML. Listing 1 implements toXml () in the class that represents the order in the online store example with JDOM API (see figure 1).
Listing 1. JDOM implementation of toXml () of the Order class
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)
Here you can see how easy it is to create elements, use attributes, and add element content with JDOM. The toXml () method of the composite JavaBean is called recursively to get the Element representation of their subgraphs. For example, the content of the items element is obtained by calling toXml () on each Item object that Order aggregates.
Once all the JavaBean have implemented the toXml () method, it is easy to serialize any object graph into an XML document and return it to the Ajax client, as shown in listing 2.
Listing 2. Generate a XML response 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 once again makes the job very easy. You just need to wrap a Document around the XML element returned by the object graph, and then write the document to the servlet response with XMLOutputter. Listing 3 shows an example of XML generated in this way, initializing the XMLOutputter with JDOM Format.getPrettyFormat () and formatting it very well. In this example, the customer placed only one order, including two items.
That's all for "how to serialize Java objects". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.