[Original] JDOM and XML analysis, Part 1

xiaoxiao2021-03-06  79

[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: Document Doc = New Document ("root");

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: this is the root Document Doc = new document (); element e = new element ("root"); E.SetText ("this is the root"; doc.addcontent (e);

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:

...

For elements, you can use naming property values ​​any name:

String Val = Table.GetaTAttributeValue ("width");

For special operations, such as type changes, you can also get an attribute as an object: attribute border = table.getattribute ("border"); int size = border.getintValue ();

Use SetAttribute () settings or change properties:

Table.setttribute ("vSpace", "0");

Use removeAttribute () to delete an attribute: Table.RemoveAttribute ("vspace");

About the text content

Element text content is just like:

a Cool Demo

In JDOM, text can be called directly:

String desc = description.gettext ();

As long as you pay attention, because the XML 1.0 specification requires blank preservation, return "/ n a cool demo / n". Of course, as actual programmers often do not want to face blank formats, there is a simple method when neglecting the surrounding blank:

String BetterDesc = description.getTextTrim ();

If you need a blank in the picture, you need a getTextNormalize () method to make a decomposition blank. It is convenient for text content:

Sometimes You Have Text Content with Formatting Space within the string.

Change the text content through the setText () method:

Description.Settext ("a new description");

Any special characters inside the text are interpreted by the corresponding character, and the space has an appropriate meaning in the output. E.g:

Element.setText (" Content");

Internal storage is saved in characters. Tore Will Keep That Literal String As Characters. There is no need to blindly decompose content. At the time of output:

Content

These behaviors maintain the semantics of early setText () calls. If you need XML being supported within an element, you must add the appropriate JDOM sub-element object. JDM can also handle the CDATA section. A text text required for a CDATA section does not need to be analyzed. It includes simple HTML and XML content without and spaces. Create a CDATA section, as long as you wrap the string with a CDATA object: Element.addContent (New CData (" Content"));

Mixing content behavior

Some elements include a lot of content, such as Whitespace, Comments, Text, Child Elements, and more:

Some Text

Some Child Element

When an element includes text and child elements, it is called mixed and content. Treatment mixing content is very difficult, but it becomes simple through JDOM. Standard application - Retrieve text content and traversal elements - very simple:

String text = Table.getTextTrim (); // "Some text" Element tr = table.getchild ("tr"); // a straight reasonnce

For most advanced users, they need notes, blank, processing instructions, and entity references, unprocessed mixed text as List is feasible:

List mixCo = Table.getContent (); item itR = mixedco.iterator (); while (itr.hasnext ()) {Object o = i.next (); if (o instanceof comment) {...} // Types Include Comment, Element, CData, Doctype, // Processinginstruction, Entityref, And Text}

As a list of child elements, changing the original list affects the background document:

// Remove the Comment. IT's "1" Because "0" is a white block block.mixedco.remove (1);

More details on DOCTYPE, Processinginstruction, and Entityref Classes can be found in the API document on Jdom.org.

Oracle XML Tool

XML Developer Kit (XDK) is a free XML tool library, which is available to developers. It includes an XML parser and an XSLT translation engine that can be used with JDOM. On the OracleXML home page, you can find more information about these tools, oracle.com / XML. Download the parser to find the XML Developer Kit called "xdk for java.". Click "Software" to find the download connection on the left column. Once you open the classification, the file Xalparserv2.jar includes a parser. Use the defaults of the parser to configure JDOM and other software, you need to set the jaxp javax.xml.parsers.saxparserfactory system for oracle.xml.jax.jxsaxparserfactory. This will tell JAXP to select the Oracle parser. The simple method is in the command line input: [code] java -djavax.xml.parsers.saxparserfactory = oracle.xml.jaxp.jxsaxparserfactory

It can also be set:

System.SetProperty ("jaxax.xml.parsers.saxparserfactory", "Oracle.xml.jaxp.jxsaxparserfactory");

In addition to XDK, Oracle provides the original XML Oracle9i Database Release 2 warehouse. Oracle9i XML Database (XDB) is a lot of XML storage. It fully absorbs the W3C XML in the data mode in Oracle9i Database and provides new standard access methods for XML traversal and queries. Regarding XDB, it has obtained the database-related advantages and the advantages of XML technology.

转载请注明原文地址:https://www.9cbs.com/read-121019.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.047, SQL: 9