18.3 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 sure that both your code and the data coming in will produce well- formed XML.

If all of that data validation sounds like a hassle, you may want to explore Genx (http://www.tbray.org/ongoing/genx/docs/Guide.html), a C library created by Tim Bray, one of the editors of the XML specification, that generates Canonical XML.


Another common approach involves using a tree-based API, such as the DOM, to create an XML document tree dynamically. The benefit of this approach is that the library enforces well- formedness constraints, and in the case of DOM Level 3, it can be configured to enforce validity constraints as well.

The disadvantage of the library approach is that it is frequently more complex and less efficient than the simple string-processing approach. This code fragment creates the same document as before but with DOM:

 DOMImplementation di; ... di.createDocument(null, null, null); Document doc; Element elMsg = doc.createElement("message"); elMsg.appendChild(doc.createTextNode("Hello, world!")); doc.appendChild(elMsg); LSSerializer lss; . . . FileWriter out = new FileWriter("message.xml"); out.write(lss.writeToString(doc)); 

This is quite a bit more complex than the simple string-based approach shown previously.



XML in a Nutshell
XML in a Nutshell, Third Edition
ISBN: 0596007647
EAN: 2147483647
Year: 2003
Pages: 232

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net