XML must be stored in a place
XML is a great data format-it is clear that the entire IBM DeveloperWorks zone is focused on this topic. In the 2007, the discussion of XML was mostly about Web services, or transformations between XML and Java™ objects, reading XML configuration files, or even using XML-formatted databases instead of relational or object-oriented databases.
For now, we haven't heard anyone talking about how to save XML in a static file with the memory represented-dom, JDOM, and so on, and fill it with angle brackets and quotes. Frankly, getting the XML and writing it to a file is not exciting-but it's necessary. What would it be like to have a programming domain that could never store XML persistence in a file? You can create an XML document in memory, and you can even send it to other components in your application (or to components of other applications), but you cannot store the XML. You can use XML to store configuration data while writing various tools to read the data, but you cannot actually store the configuration file itself. You can even read the contents of the SOAP envelope-but you cannot store the content on disk for the application to take offline.
Obviously, writing XML to a file is very important. In fact, if you just want to keep your data in memory and you don't need to worry about how it's stored, you can imagine what a programming world without the need to store XML is, without a doubt, impossible in today's programming universe.
So the question is simple: how do you store XML persistence in a file? I assume that the reader of this article needs to handle this task on its own. In other words, if you have never been programmed to involve persistent storage XML, this article will benefit you. (However, knowing how to perform these tasks is more conducive to understanding the article) for those who do focus on persistent storage, I've summed up three fairly popular mainstream approaches:
Use APIs such as DOM and JDOM to write XML data structures directly to files
Persisting storage of your XML using the transformation API for XML (TrAX) and Identity Transformation (identity transformation)
Handling persistent storage using higher-level APIs such as JAXB
Direct use of APIs
If you use one or more APIs to read XML, one obvious way is to use the same APIs to write XML to the file. For example, if you use the JDOM API and a JDOM Document object to manipulate XML, you can write the following code:
XMLOutputter outputter = new XMLOutputter();
outputter.setFormat(Format.getPrettyFormat());
outputter.output(myDocument, new FileWriter ("outputFile.xml"));
Similarly, the new Load and Save APIs can be used in DOM Level 3:
DOMWriter writer = new org.apache.xml.serialize.XMLSerializer();
writer.setNewLine("\r\n");
writer.setEncoding("UTF-8");
writer.writeNode(new FileOutputStream(new File("outputFile.xml")), myDocument);
Note that there are a variety of ways to use the new DOM APIs, some of which have lower vendor independence. The example code above contains a Xerces class, but other methods are not tightly bound to a particular vendor class as it does. From a learning standpoint, those methods are not intuitive enough, so I keep the vendor-specific code.
Advantages
The advantage of this approach is that you can interact fairly directly with your API to achieve good and comprehensive control. You can set up new rows, you can work with first-line indents, and you can control all aspects of the output file. In addition, you can close the distance between you and the file as much as possible, that is, without the wrapper API or the indirect layer, you can write XML directly. If you are familiar with JDOM and DOM, they will be the right choice for exporting XML.
Disadvantages
Any method has its advantages, it must have disadvantages. Although you can fully control the various details of the output, if the output is improperly configured, it can cause a lot of confusing problems. Line-wrapping errors, writing errors, and I/O errors are some of the common problems that this method produces. In addition, you work at a very low level and do not have a large number of accessibility tools (JDOM in the Format.getprettyformat () and Format.getcompactformat () methods: While DOM is almost skinflint). This means you have to understand the encoding, output format, indentation format, and anything that affects the output.