Generating XML Documents
One area of XML development that isn’t often addressed is that of generating XML documents for consumption by other applications. Although there are several approaches for processing XML documents, there are relatively few techniques currently used to create new documents.
One of the simplest (and most common) approaches is to use the string and/or file processing facilities of your target development environment to construct the XML document directly. This approach has the benefit of being easy to understand, efficient, and readily accessible to every programmer. This Java statement emits a simple XML document to a file output stream:
FileWriter out = new FileWriter("message.xml");
out.write("<message>Hello, world!</message>");It’s not hard to see how this approach would be implemented in any other programming language. For example, in C++, the following statement creates the desired result:
ofstream fout;
fout.open("message.xml", ios::app);
fout << "<message>Hello, world!</message>";This is a completely valid approach, and it should be considered when the XML document is not overly complex and the structure of the document will not change substantially over the lifetime of the application. The disadvantage of this approach is that it is much easier to generate a document that is not well-formed or is invalid, since no validation or verification of the structure of the document occurs as it is generated. When using this technique, you of course have to make ...