Easily integrate Java and XML (2) with JDOM
Get an XML document
JDom describes an XML document with an instance of the org.jdom.Document class. This document class is a lightweight level, which can include document types, objects, one root elements, and annotation objects for multiple processing instructions. You can construct a document from the draft without a constructor.
Document Doc = New Document ("RootElement");
In the next article, we will discuss how easy it is to construct an XML document from a draft. But now, we construct our document from an existing file, a stream, or a URL path.
SAXBUILDER Builder = New Saxbuilder (); Document Doc = Builder.build (URL);
You can construct a document from any data source with the constructor provided in the org.jdom.input package. Recently, there are two construction methods: SAXBUILDER and DOMBUILDER. SaxBuilder constructs a document from the file with the SAX parser. SAXBuilder listens for the SAX event and creates a corresponding document from memory. This method is very fast (basically as fast as SAX), which is also our recommended way. DomBuilder is another optional way that creates a JDOM document from an existing Org.W3c.Dom.Document object. It allows JDOM to easily implement interfaces for tools to build DOM trees.
JDOM speed has the improved potential to increase the completion of a delayed constructor. This constructor checks the XML data source, but parsing it when the request is requested. For example: the attribute of the document does not need to be parsed when not access.
The constructor is still developing, which can make a JDOM documentation through SQL query, LDAP query, and other data formats. So, once you enter memory, the document is not related to the tools to build it.
SAXBUILDER and DOMBUILDER constructors allow users to indicate whether they should be rotated to determine which parser time performs parsing tasks.
Public Saxbuilder (String Parserclass, Boolean Validation); PUBLIC DOMBUILDER (String Adapterclass, Boolean Validation);
The default is the open source XERCES parser with Apache and is turned off. You should note that DomBuilder is not like a parsing class, and it is more like an adaptation class. This is because not all DOM parsers are the same API. In order to still let users choose a favorite parser, JDOM uses a common API that is applicable to all DOM parsers. This adaptation class supports all popular DOM parsers, including apache's Xerces, Crimson, IBM's XML4J, Sun's Project X, and Oracle's Parsers V1 and V2. Each parser passes the correct call other parser Execute a standard interface. This is a bit like JAXP, in addition to the new parser that JAXP does not support.
Output XML document
You can use several different standard output tools to output an XML document. Org.jdom.output.xmloutPutter may be the most common method. It writes an XML document to a specific OutputStream.
The SAXOUTPUTTER tool is another option. It produces SAX events based on JDOM XML documents, you can send them to applications waiting for these SAX events. In the same way, DomoutPutter produces a DOM document so you can give it to the application that can receive the DOM document. The code to output the XML document looks like the look below:
XmloutPutter Outputter = new xmloutputTer (); Outputter.output (DOC, System.out);
XMloutPutter is customized with parameters. The first parameter is the format of a row; the second parameter is whether you think of another line. Because of the machine to the machine, you can give up the line indentation and further new rows for speed considering.
XMloutPutter Outputter = new xmloutputter (", false); Outputter.output (DOC, System.out);
(Translation: This kind of thing you will look very well, and every time you rewrite, you will make this XML file bigger, so I suggest it still need to indent and connect, support Chinese with wrap and indentation. The format is as follows:
XMloutPutter Outp = new xmloutputter ("", true, "gb2312");
Outp.setTextTrim (TRUE);
Outp.output (DOC, System.out);
You can see the JDOM of the previously wrote http://www.9cbs.net/develop/read_article.asp?id=20720)
The following is an example of reading an XML document and outputs it again:
Import java.io. *; import org.jdom. *; import org.jdom.input. *; import org.jdom.output. *; public class prettyprinter {public static void main (string [] args) {// assume filename argument String filename = args [0]; try {// Build the document with SAX and Xerces, no validation SAXBuilder builder = new SAXBuilder (); // Create the document Document doc = builder.build (new File (filename)) ; // output the document, use standard formatter xmloutputter fmt = new xmloutputter (); fmt.output (doc, system.out);} catch (exception e) {E.printStackTrace ();
Resources:
Read Part 2 of "Easy Java / XML Integration with Jdom," Jason Hunter and Brett Mclaughlin (Jason Hunter and Brett McLaughlin (July, 2000) To learn how to use jdom to create and mutate xml: http://www.javaworld.com/javaworld/jw- 07-2000 / JW-0728-jdom2.html
The home of jdom: http://jdom.org/
Mailing List Sign-Up for Jdom-Interest And Jdom-Announce, AS Well As List Archives: http://jdom.org/incolved/lists.html
The jdom announcement press release: http://www.oreillynet.com/pub/a/mediakit/pressrelease/20000427.htmlmore Information on Dom: http://www.w3.org/dom/
More information on sax: http://www.meginson.com/sax/
More information on jaxp: http://java.sun.com/xml/