Parse XML using JDOM

Source: Internet
Author: User

XML (EXTensibleMArkupLAnguage)-extensible markup language. You can define language markup by yourself. You only need to enable or disable tags.

During development, developers use XML files every day. But how can they read these files? There are many related technologies, such as traditional Dom, Sax and later JDOM and dom4j. In fact, dom4j was the first to come into use, but later I found JDOM in my study. In contrast, the two are quite similar. The following is a brief introduction to JDOM.


JDOM is an API function that parses, generates, serializes, and operates on XML documents using pure Java technology. In JDOM, the XML element is the element instance, the XML Attribute is the attribute instance, and the XML document itself is the document instance. The specific operations are as follows:

First, you need to prepare jar packages related to JDOM.

JDOM-jar: http://www.jdom.org/dist/binary/

Next we need to introduce the relevant jar package, here I use the jdom-1.1.3



1. Create an XML file using JDOM

Public class createxmlbyjdom {public static void main (string [] ARGs) {// create a document instance document = new document (); // create a root element contactlist, and create the attribute element root = new element ("contactlist "). setattribute (new attribute ("company", "acompany"); // Add the root element to the Document Object. addcontent (Root); // create the element linkmanelement contactperson = new element ("linkman"); // Add the element to the root element root. addcontent (contactperson); // create a child element contactperso N. addcontent (new element ("name "). settext ("rain falls to the horizon ")). addcontent (new element ("company "). settext ("chuangyu group ")). addcontent (new element ("telphone "). settext ("13745628456 ")). addcontent (new element ("Address "). addcontent (new element ("street "). settext ("Chaoyang Street ")). addcontent (new element ("city "). settext ("Beijing"); // use xmloutputter to produce the XML document xmloutputter xmlout = new xmloutputter (); try {// call the output method of the xmloutputter object to generate the XML document Xmlout. Output (document, new fileoutputstream ("contact. xml"); system. Out. println ("Writing XML successed! ");} Catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();}}}


Generated XML file:


Ii. Use JDOM to read XML files

First, create the sample. xml file.


Read the XML file as follows:

Public class sample {public static void main (string [] ARGs) throws exception {// create a saxbuilder object saxbuilder sb = new saxbuilder (); // treat the entire document as an object document DOC = sb. build (sample2.class. getclassloader (). getresource ("sample. XML "); // root obtains the root object element root = Doc of this document. getrootelement (); system. out. println (Root); // get the HD/disk node list of the root object = XPath. selectnodes (root, "/HD/disk"); system. out. println (list. size (); // traverse the child element set of the root element (that is, traversing the list element) for (INT I = 0; I <list. size (); I ++) {element disk_element = (element) list. get (I); string name = disk_element.getattributevalue ("name"); string capacity = (text) XPath. selectsinglenode (disk_element, "// disk [@ name = '" + name + "']/capacity/text ()")). gettextnormalize (); string directories = (text) XPath. selectsinglenode (disk_element, "// disk [@ name = '" + name + "']/directories/text ()")). gettextnormalize (); string files = (text) XPath. selectsinglenode (disk_element, "// disk [@ name = '" + name + "']/files/text ()")). gettextnormalize (); // The output shows the XML data system. out. println ("disk information:"); system. out. println ("partition drive:" + name); system. out. println ("partition capacity:" + capacity); system. out. println ("number of directories:" + Directories); system. out. println ("number of files:" + files); system. out. println ("---------------------------------") ;}< span style = "font-family: fangsong_gb2312;" >}</span>


Final display result:



3. Comparison between JDOM and dom4j:


JDOM and dom4j are both developed for the Java language, and JDOM is the first specific Java model. Therefore, many performance aspects are obviously inferior to the subsequent dom4j. For example, first, JDOM only uses a specific class instead of an interface. This simplifies APIs in some ways, but also limits flexibility. However, dom4j uses a lot of interfaces and abstract basic class methods. Second, JDOM does not contain a parser. Generally, the sax2 parser is used to parse and verify the input XML document.


Parse XML using JDOM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.