Generate XML with Java

xiaoxiao2021-03-06  61

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 In this simple example, if the name is quirky, such as Jon "the cat" smith, the output format is problematic. . This code did not process quotes when the name was acquired. The result of the result output became like this, apparent that this representation is wrong: It is difficult to track XML hidden in Java when you read the source code. It is said that about 1 half of the error in this development measures are attributed to the quotation of the closed label and the wrong quotation, in short, it is invalid to XML. More clear and simple methods: DOM 2nd method is DOM, which is the so-called document object mode (Document Object Model). After a given object structure, you can convert it to a format XML object structure and then output the result. There are many types of structural types, including the XML class of Jakarta Element Construction Kit (ECS) projects to fully comply with DOM specification, such as Xerces, usually, the smaller version, the smaller the output XML method is simpler. Listing B is an example of using ECS. Listing B proposes a concise approach to output data. In fact, you can merge two output lines into one, and the method is to attach the Output method after new XMLDocument. This is a kind of syntax mode using ECS, which is convenient to use. However, this approach does not have a good replication of IF-NULL protection against the age of people. In order to achieve this protection, you must use the code in the list C. ECS shows several advantages, you don't have to avoid quotation marks ("). You don't need to completely enclose the label, the object will do these work for you, any XML characters, such as is instead, ECS can be described as the simplest DOM method.

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.

转载请注明原文地址:https://www.9cbs.com/read-82130.html

New Post(0)