Jdom introduction and use guide
First, JDM introduction JDOM is an open source project, which is based on the tree structure, using pure Java technology to resolve, generate, serialize, and multiple operations for XML documents. JDOM is directly programmed for Java. It utilizes more features of more powerful Java languages (method overload, collection concepts, and mapping), effectively combining SAX and DOM functions. Hide the original use of the XML process as designed to use the complexity of the XML process. Using JDOM to handle XML documents will be a relaxed, simple matter. JDOM was developed by Brett McLaughlin and Jason Hunter in the spring of 2000 to make up for the shortcomings of Dom and SAX in practical applications. These deficiencies are mainly in SAX no document modification, random access, and output, and for DOM, Java programmers will always be very convenient to use. Dom's shortcomings are mainly derived from DOM is an Interface Definition Language (IDL). Its task is a minimum general standard in different language implementations, not specially designed for Java. The latest version of JDOM is JDOM Beta 9. Recently, JDOM is included in JSR-102, which marks a part of JDOM a Java platform.
Second, the JDOM package overview JDOM is made of org.jdomorg.jdom.inputorg.jdom.jdom.inputorg.jdom.Outputorg.jdom.Adaptersorg.jdom.jdom.Adaptersorg.jdom.jdom.Adaptersorg.jdom.jdom.Adaptersorg.jdom.jdom.Adaptersorg.jdom.jdom.Adaptersorg.jdom.jdom.Adaptersorg.jdom.jdom.Adaptersorg.jdom.jdom.Adaptersorg.jdom.transform
Third, JDOM class description
Org.jdom This package is the type of data you want to use after you resolve the XML file. AttributeCDataComentDoctypedocumentElemenTentityRefNamespaceProscessinginstructionText
Org.jdom.Transform should use the following 2 class JDomsourcejDomResult when transitioning to XSLT format.
Org.jdom.Input Enter the class, generally used for the creation of the document SAXBuilderDombuilderResultSetBuilder
Org.jdom.output output class for document conversion output XMLOUTPUTTERSAXOUTPUTTOUTPUTTERJTREEOUTPUTTER
Precautions Preview: 1.jdom For JAXP and TRAX support JDOM support JAXP1.1: You can use any Parser tool class in the program, by default, JAXP Parser. Develop special Parser available SAXBUILDER PARSER = New Saxbuilder ("org.apache.crimson.Parser.xmlReaderImpl"); document doc = parser.build ("http://www.cafeconleche.org/"); // Work With the document ... JDOM also supports Trax: XSLT can be converted through JDomSource and the JDomResult class (see later chapter) 2. Note that in the JDom documentation (Document) class is represented by org.jdom.document. This is to distinguish between Document in org.w3c.dom, how these two formats are converted later. The following is as not specifically referred to as Document in the JDOM.
Fourth, JDOM main use method 1. Cucle class (1) Document method: element root = new element ("greeting"); document doc = new document (root); root.settext ("Hello Jdom!"); Or Simple Using Document Doc = New Document ("Greeting"). SetText ("Hello Jdom! T")); this is different from DOM. Dom will need more sophisticated code, as follows: DocumentBuilderFactory 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);
Note: JDOM does not allow the same node to be associated with 2 or more documents, and use the original old document in the second document. First, you need to use DETACH () to separate this node. (2) Get the document, stream, system ID, and URL to get Docuilder Builder = New Dombuilder (); Docuild Doc = Builder.Build (New File ("jdom_test.xml");
SAXBUILDER Builder = new saxbuilder (); document doc = builder.build (URL); Dombuilder has deprecated DomBuilder.builder (URL) in the new version, using SAX efficiency.
Here is a small example. For the sake of simplicity, use String objects directly as an XML data source:
Public jdomtest () {string textXML = null; TextXML = "
(3) DOM's Document and JDOM Document mutual conversion method, simple! Dombuilder Builder = New Dombuilder (); org.jdom.document jdomdocument = builder.build (domdocument); // Work with the jdom document ...
DomoutPutter Converter = new domoutputter (); org.w3c.dom.document domdocument = converter.output (jdomdocument); // Work with the document ...
2.XML Document Output XMLOUTPUTTER Class: JDOM's output is very flexible, supporting many IO formats and style output Document Doc = new document (...); xmloutputter outp = new xmloutputter (); // raw outputoutp.output (DOC) , fileOutputStream); // Compressed outputoutp.setTextTrim (true); outp.output (doc, socket.getOutputStream ()); // Pretty outputoutp.setIndent ( ""); outp.setNewlines (true); outp.output (doc , System.out; ...... Please refer to the latest JDOM API manual.
3.Element Class: (1) Browse Element Tree // Get Root ElementElement Root = Doc.GetrooteElement (); // Get all sub-elements of a ListList AllChildren = root.getchildren (); // Get specified name sub-elements Listlist namedchildren = root.getchildren ("name"); // Get the first child element element child = root.getchild ("name") of the specified name; (List here is java.util.list) JDom gave us Many very flexible use methods to manage child elements list allchildren = root.getchildren (); // Delete the fourth child element allchildren.remove (3); // Remove the Subrid of "Jack" Allchildren.Removeall (root.getchildren ("jack"));
Root.removechildren ("jack"); // Convenient write method // Add AllChildren.Add (New Element ("Jane"));
Root.addContent (New Element ("Jane")); // Convenient Writing Allchildren.Add (0, New Element ("first"));
(2) Moving Elements: Simple Element Movable = New Element ("Movable") in JDOM; Parent1.addContent (Movable); // PlaceParent1.RemoveContent (Movable); // RemoveParent2.addContent (Movable); // Add
Element movable = doc1.createElement ("Movable") in DOM; Parent1.Appendchild (Movable); // PlaceParent1.RemoveChild (Movable); // RemoveParent2.Appendchild (movable); // Error!
Supplement: Element constructor of the error correction JDOM (and other functions) will check if ELEMENT is legal. And its Add / Remove method checks the tree structure, the inspection content is as follows: 1. Is there a round-link node in any tree 2. Is there only one root node 3. Is there a consistent namespace (Namespaces)
(3) ELEMENT's Text Content Read
/// Ret IS Directly Available // Returns "/ N A Cool Demo / N" String Desc = Element.getText ();
// there's a convenient shortcut // returns "a cool demo" string desc = element.gettexttrim ();
(4) ELMENT content modifies Element.Settext ("a new description"); 3. Correctly explain the special character Element.setText ("