Easily integrate Java and XML with JDOM (3)

zhaozj2021-02-17  49

Easily integrate Java and XML with JDOM (3)

Read document type

Now let's take a look at how to read the details of the document. One thing in many XML documents is a document type, which is described in a DOCTYPE class in JDom. In case you are not an expert in XML (, don't be discouraged, you are the listeners we have to face), a document type statement looks like the look:

The first word behind DOCTYPE reveals the name of the mandatory type, the word behind the public is the public property of the document type, and the last word is the system properties of the document type. Document properties can be obtained on the GetDocty () method of the document, and the DOCTYPE class provides a set of methods for obtaining document type declarations.

DOCTYPE DOCTYPE = doc.getdoctype (); System.out.Println ("Element:" DOCTYPE.GETELEMENTNAME ()); System.out.Println ("Public ID:" DOCTYPE.GETPUBLICID ()); system.out. Println ("System ID:" DOCTYPE.GETSYSTEMID ());

Read document data

Each XML document must have a root element. This element is the starting point to access all the internal information of all XML documents. For example: This document fragment uses as the root element:

Gotta Fit Servlets in Somewhere!

Examples of root elements can be obtained directly in the document.

Element WebApp = doc.getrootelement ();

This way, you can access the properties of this element (such as the ID) content and child node elements.

Access child node

The XML document is a tree structure, and any element may contain any number of child elements. For example: elements are and as child node elements. You can get a child element of an element in many ways, and getChild () returns NULL if there is no child element.

List getchildren (); // return all childrenlist getchildren (String name); // Return All children by nameElement getChild (String name); // return first child by Name

Example:

// Get a list of all Direct Children As Element Objects list allchildren = element.getchildren (); out.println ("First Kid:" (Element) Allchildren.get (0)). GetName ()); // Get a list of all Direct children with a given name list namedchildren = element.getchildren ("name"); // Get a list of the first kid with a given name element kid = element.getchild ("name"); as documentation In advance, the getChild () method is easy to get nested elements quickly. Give an XML document:

Enlightenment 0.16.2

The following code directly gains the name of Window Manager

String WindowManager = RootElement.getchild ("GUI") .Getchild ("Window-Manager") .Getchild ("name") .getText ();

If the documentation is not available to be careful NullPointersExceptions exception. In order to achieve a simple document navigation, the future JDOM may support XPath. The child node can get the parent node through getParent ().

Get document properties

The attribute is another group of information that the element has. HTML programmers are very familiar with him. The

element below has a width and border properties.

These properties can be obtained directly in the element.

String width = Table.getaTRibutevalue ("width");

You can also re-get these properties with attribute instances. This capability helps JDom support some advanced concepts, such as properties in the namespace. (Refer to the article on the name of the name)

Attribute widthattrib = table.getattribute ("width"); string width = widthattrib.getValue ();

For your convenience you can also get the original data type of these properties.

Int width = table.getattribute ("border"). getInetValue ();

You can transform these data to any raw data type. If these attributes cannot be converted to the original data type, you will throw a DataConversionException exception. If the property does not exist getAttribute () returns a NULL;

Extract documentation

We are excited to get document content in a simple method, and then look at how easy it is to extract the text content of the document with the Element.getText () method. This is a standard method for documents like the following:

enlightenment

But some of these documents contain comments, text content and child elements. In some advanced documents, it even contains some processing instructions:

- Some Comment -> Some text some child

You can always get text content and child nodes by the following ways:

String text = Table.getText (); // "Some text" element tr = table.getchild ("tr"); //

child

This makes the standard use very simple. Sometimes, for example, output, it is important to obtain the order of all content of a document. For this reason, you can use a special method called getMixedContent (). It returns a list content that may contain annotations, strings, elements, and processing instructions. Java programmers can use InstanceOf to get content. Low code prints a summary of a document content: List mixedcontent (); item i = mixContent.iterator (); while (I.hasNext ()) {Object o = i.next (); if (o instanceof Comment) {// Comment Has A toString () Out.println ("Comment:" O);} else if (o instanceof string) {Out.println ("string:" O);} else if (o instanceof {OUT.PRINTLN ("Pi:" ("pi:" ) .Gettarget ());} else if (o instanceof element) {Out.println ("Element:" o) .getname ());}}

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/

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

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