Under normal circumstances, we will focus on parsing XML and XML structures as as long as we mention XML. In such technical fields, W3C proposed DOM and SAX specification to parse data, and Sun provides Java XML Pack, and Apache has introduced Xerces and Xalan. However, there is almost no attention to your attention to the problem of output XML. There are some items that turn the JavaBeans and Swing components into XML, but in most cases, developers only want to output data structures with customized formats, this task is not difficult. This article specially explores some methods for creating an XML document through Java. I will specifically mention several scenarios for creating XML documents with Java. They all have different advantages, some are very simple, and some will depend on certain powerful class libraries. Below I am from the simplest method. The simplest use of the StringBuffer class is the simplest, and the most commonly used XML document creation method is your own. You can use StringBuffer classes or some Writer classes. The advantage is that you don't need to use other libraries, and you don't have to create extra objects. However, this initiative also brings a lot of shortcomings. It is the first to say that the correctness of XML cannot be guaranteed. Characters must be carefully considered when placing the String object. You have to be particulartious about the appearance of the XML entity, such as
Use W3C's DOM, JDOM or DOM4J to deal with this style of code, and of course, W3C's DOM still has the advantages of independent resolution. The disadvantage of using the ECS output XML includes the problem arising from the object. You must establish an object before writing content. This is still nice in most cases, but you don't want to build this XML structure when you output a large XML file. The same problem exists in most of the other DOM methods. The ECS is quite close to one mark compared to simply use Writers or StringBuffer classes. Its volume is relatively large, but only a small part is used to output XML. The biggest problem is that its activity is small. It beats other similar programs because other types are larger, more cumbersome and more complicated. SAXSAX (Simple API for XML) can replace the DOM style XML resolution. It consists of a series of events or code consists of a callback function called when the XML file is parsed. In the case where you use the results directly to Strings, it does not send how big can you send. However, it can be used in indirect, more complicated ways. The code that outputs Strings can replace the output SAX event. This trick is much higher than that only output Strings, which can be added to a simple base class to convert the SAX event to XML. Let's take a look at the following examples, this example uses the following classes: • Person: Business object, previously explained • PersoninPutSource: Accommunicate • PersonXMLReader: Know how to convert the personInInPutSource to SAX event • Xmlprettyprinter: The SAX event is converted to XML's ContentHandler's most important code in PersonXmlReader, as shown in Listing D. The code in List D illustrates how the Person object is converted to a series of SAX events. But this is not the simplest to live. Convert PERSON to XML with SAX to be implemented by the code in the list E. With SAX clearly keeps you a powerful computing power because you can come with the SAX parser on XML without using XMLPrettyprinter. However, it will also increase complexity when adding processors; SAX is a more complex concept. In most cases, a simple method is often the best effect. Once the basic components are written (XmlPrettyprinter is a basic InputSource, an XMLParser object), the event is a small matter. Output new XML structure only requires a Parse method and inserting components. Establishing XML around SAX event is not a fast, convenient measures. I use the XMLWRITER class. I finally put forward my own scheme. This is the XMLWRITER class. My idea is to use a technology that is bound to be too simple and too complicated to output XML. Important design requirements are as follows: • Package Java.io.writer • Provides an API of the Writer class • To process XML as much as possible • Avoid building large object structure • Allow ECS chain style to achieve these needs to make XMLs into two styles. First, it can be written with java.lang.writer code, as shown in Listing F. Second, it can be written with the encoded style of a chain method, which is similar to ECS because each Write method returns to XMLWRITER itself. Listing G gives this example. From a performance perspective, XMLWRITER has a significant advantage that it has little to create other objects. It has considerable functionality that can handle basic XML fragments (but no comments, regulating, or document types). Most importantly, it is very simple. The negative problem has been mentioned, it cannot handle annotations, regulating, or document types.