[Original] JDOM and XML analysis, Part 1
Jdom let XML are easily used in Java.
Previously, a part of Java's library class may be used to operate the XML data structure. So, what is the point of JDOM (Java Document Object Model, why do developers need it? JAVA-OPTIMIZED XML data operation JDOM is open source. Although it is very similar to the World Wide Web Alliance (W3C) DOM, it is an object model that is optional, this model is not based on DOM or DOM-based model. The main difference is that DOM is established as Language-Neutral and is used for JavaScript operations in the HTML page, and JDom is established as Java-Specific, so it has the advantages of Java itself, including Method Overloading, Collectes, Reflection, And Familiar. For Java programmers, JDOM tends to explore more capabilities and benefits. This is very like Java-Optimized RMI (Remote Method Invocation) explores more its own ability, not like Language-Neutral Corba (Common Object Request Broker Architecture).
In the Open Code Apache-style (Commercial-Friendly) version on JDOM.ORG, you can find JDOM. It is a collaborative design, developed product, which has more than 3,000 volunteers. This library is also accepted by Sun's Java Community Process (JCP) as Java Specification Request (JSR-102), while step by step is a formal Java specification.
The following series of articles will provide technical description for JDOM. The article provides information about important classes. The next article will give some Java programs to use the JDom content. JDOM package structure
JDOM libraries consists of six packages. The first Org.jdom package supports an XML document, including: Attribute, CData, Comment, DOCTYPE, Document, Element, Entityref, Namespace, Processinginstruction, and Text. If you are very familiar with XML, the class name will help the meaning of the decline. The ORG.JDOM.INPUT package is supported, it supports classes to establish an XML document. The most important class is SAXBUILDER. SAXBuilder creates documents by listening to an API for XML (SAX) events. When you want to create a document from a file or other stream, you can use SaxBuilder. It uses the SAX analyzer to read the current, and then establish a document according to the return of the SAX analyzer. A good aspect of this design is the faster the analyzer's work, the faster the SAXBUILDER work. Another major input class is DomBuilder. Dombuilder is established from the DOM tree. This class is easy to get from the previous DOM tree, and a JDM version is replaced. . It is not limited to the designer. For example: Xerces now operates Xerces Native Interface (XNI) when it is low-level than SAX, which can make judgments to do XNibuilder to support some rules of analyzers, not exposed by SAX. ResultSetBuilder is a popular person who contribute to the JDOM project. This makes JDBC more robust and establishes SQL XML document expressions, including many of those that are components that are attributes. Org.jdom.outpu package support class output XML document. The most important class is XMLOUTPUTTER. It converts it to a byte stream in order to output documents to files, streams, and sockets. The XMLOUTPUTTER has a lot of strange configuration options to support the original output, appropriate output, or compressed output and other means. It is a fairly complex class. This maybe why this is not in the Dom Level 2. An additional output includes class SAXOUPPUTTER, which generates a SAX event based on the contents of the document. Although it is very mysterious on the surface, this class is very useful in XSLT conversion because SAX events are more efficiently transmitted more efficiently than bytes. There are also class DomoutPutter here, which established the DOM tree documentation. An interesting design is JTreeoutPutter, which only has few code, which has established JTREE to document representation. With ResultSetBuilder, use simple code to complete SQL query and queue traversal. Note that unlike DOM, developers will not pack documentation. This creates a model that can be used to save data, and many classes construct data, and many other classes abandon data. Org.jdom.Transform and Org.jdom.xpath packets support to establish XSLT conversion and XPath lookup. Finally the Org.jdom.Adapters package supports communication within the DOM. Users do not need to access the class of this library. Each DOM execution process has a different method name for each step, so the adapter converts the standard call to Parser-Specific calls. Java API for XML Processing (JAX) gives some other solutions for this issue, actually for the needs of the class, but some classes remain, because all analyzers support JAXP, or JAXP is not anywhere installed anywhere The version is correct.
Create a document document by org.jdom.Documentclass to assist the establishment. You can build a document like this: // this builds:
Or can create a document by a file, stream, system ID, or URL:
// this builds a document of whatver's in the given resourcesaxbuilder builder = new saxbuilder (); document doc = builder.build (URL);
Some calls are combined in JDO, you can easily create a simple document:
// this builds:
If it is an experienced user, you may use "Method Chaining,", and is called in turn in turn when there are multiple methods. Because the fixed method returns the generated object. as follows:
Document Doc = New Document (New Element). SetText ("this is the root");
As a comparison, here is given the same documentation using JAXP / DOM:
// JAXP / DOMDocumentBuilderFactory factory = DocumentBuilderFactory.newInstance (); DocumentBuilder builder = factory.newDocumentBuilder (); Document doc = builder.newDocument (); Element root = doc.createElement ( "root"); Text text = doc.createText ( "This is the root"; root.appendchild (text); doc.appendchild (root);
Establish Saxbuilder
Early, SAXBuilder established a mechanism that can establish documents from any byte-oriented resource. The default saxbuilder () uses JAXP in the background to select the SAX parser. If you want to change the parser, you can set the javax.xml.parsers.saxParserFactory system feature points to SaXParser Factory, and the analyzer is guaranteed. For Oracle9i Release 2 XML parsers, you can use this:
Java -djavax.xml.parsers.saxparserfactory = Oracle.xml.jaxp.jxsaxparserFactory Yourapp
For the Xerces parser, you can replace this:
Java -djavax.xml.parsers.saxparserfactory = org.apache.xerces.jaxp.saxparserfactoryImpl yourapp
If JAXP is not installed, SaxBuilder defaults to Apache Xerces. Once a SaxBuilder instance is established, you can set some props, including: setvalidation (Boolean Validate)
This method tells the parser in the establishment to be valid for Document Type Definition (DTD). The default is closed. Using DTD is a reference for DOCTYPE of the document. It is impossible for other DTDs because there is no parser to support this capability.
SetignoringeElementContentWhitespace (Boolean ignoring)
This method tells the parser to ignore the element directory that calls Whitespace. Each XML 1.0 description, WHITESPACE in the element directory must be saved, but for a DTD to confirm that the parser knows that the part of some documents is not declared to support Whitespace, any Whitespace in that area is "Ignorable." "This default is closed. This is not the best for partial storage unless you want to "Round Trip", and the input is used as an output. Note that this tag is only activated when it is confirmed, and the confirmation makes the execution speed slow down, so it is best to use it in the case where it has been used.
SetFeature (String Name, String Value)
This method makes some settings in the background of the SAX parallel. This is a non-processing transfer, so it is very careful because setting errors (such as Tweaking Namespaces) will destroy JDOM. In addition, there is a certain convenience depending on any Parser-Specific. This call is useful for the authorization of planned confirmation.
SetProperty (String Name, Object Value)
This method makes some settings in the background of the SAX parallel. This is also a non-processing transfer, which has high hazardous and high applicability for advanced users, especially for planned confirmation. The method combines the method, the following code reads the local file using the JAXP-SELECTED pars if Ignorable Whitespace is confirmed.
Saxbuilder Builder = New Saxbuilder (); Builder.SetValidation (TRUE); Builder.setogNGElementContentWhitespace (TRUE); Document Doc = Builder.Build (New File ("/ tmp / foo.xml));
Establish a document via xmloutputter
A document can be output in a variety of formats, but the most commonly used is byte stream. In JDOM, the XMLOUTPUTTER class provides this feature. The default constructor attempts to output documents stored in memory without errors. The following code provides a representation of the original representation.
// raw outputxmloutputter outp = new xmloutputter (); outp.output (doc, filestream);
If you worry about Whitespace, you can clear your blank:
// compressed outputoutp.setTextTrim (True); Outp.output (DOC, SocketStream);
If you want to print the style of people's habits, you can add the indentant blank and change to the new line:
Outp.setTextTrim (TRUE); OUTP.SETINDENT (""); Outp.setNewlines (TRUE); Outp.output (DOC, System.out); You need to confirm that you can clean it when printing a formatted a blank document. Otherwise, it must be formatted, which may be difficult.
Operation element tree
JDOM makes the operation element tree easily. Get root elements:
Element root = doc.getrootElement (); to get a list of all its child elements: list allchildren = root.getchildren ();
Get the specified element through the name:
List namedchildren = root.getChildren ("name");
Get the first element according to given the name:
ELEMENT CHILD = root.getchild ("Name");
getChildren () calls the list returned is a java.util.list, and a list of the LIST interfaces know all the Java programmers. The list of activity is very interesting. Any List change will be displayed in the background of the background.
// Remove the fourth childallChildren.remove (3); // Remove children named "jack" allChildren.removeAll (root.getChildren ( "jack")); // Add a new child, at the tail or at the headallChildren.add (New Element); Allchildren.Add (0, New Element ("jill"));
Using List means that there is no need to add a number of methods to operate a plurality of elements. For the sake of convenience, whether in the final adding element or deleting a named element, the element itself has a corresponding method, no need to get list before operation:
Root.removechildren ("jill"); root.addcontent (New Element ("jenny");
One advantage of Jdom is that it can easily move elements within the document or between the document. The code is the same: Element Movable = New Element ("Movable"); Parent1.addContent (Movable); // PlaceParent1.RemoveContent (Movable); // RemoveParent2.addContent (movable); // Add
Using the DOM, the moving element is not easy, because in the DOM, the element is binding with the tool. Thus, the DOM element moves between the documents must be "imported". About JDOM must pay attention to the moving element to be added first, so you can't create a circular tree. There is a detach () method to separate / add a line:
Parent3.addContent (movable.detach ());
If you have forgotten it before adding an element to another, the library will throw an exception. The library also checks the names and content of the elements, confirming that they do not include inappropriate characters, just like blank. It also has other rules, such as only one root element, compatible namespace declaration, notes and CDATA sequences, and so on. This feature makes it possible to check the "Well-formedness" error as early as possible in the process. Operation element attribute element properties: