Java Web Learning notes -- use Jdom to parse xml and javawebjdom

Source: Internet
Author: User

Java Web Learning notes -- use Jdom to parse xml and javawebjdom

 

I. Preface

What is Jdom?

Jdom is an open-source project that uses java-only technology to parse, generate, serialize, and operate XML documents based on the tree structure. It is a direct java Programming Service, using the features of the java language (method overload, set), combining the functions of the SAX and DOM, as much as possible to make the original parsing xml easier, parsing xml with Jdom is easy.

Advantages of Jdom:

1. Jdom is dedicated to java technology and uses less memory than Dom applications.

2. Jdom provides basic methods for simpler and more logical access to xml Information.

3. In addition to xml files, Jdom can also access other data sources. For example, you can create a class to access data from SQL query results.

Composition of Jdom:

Jdom consists of 6 packages

Element class indicates the elements of an XML document

Org. jdom: basic class used to parse xml files

Org. jdom. adapters: contains Java classes adapted to DOM.

Org. jdom. filter: filtering class containing xml documents

Org. jdom. input: contains the Java class for reading XML documents.

Org. jdom. output: class containing the output XML document

Org. jdom. trans form: contains the Java class that converts the Jdom xml document interface to other XML document interfaces.

 

What is xml?

Xml is a widely used extensible markup language. There are many ways to parse xml in java, the most commonly used are jdom, dom4j, And sax.

 

Jdom package download: http://www.jdom.org/downloads/index.html

Here, the author's code is to use java to create an xml file and read an xml file, which is only used as a note.

Ii. Operations

Download the jdom package, unzip the file jdom-2.0.6.jar, jdom-2.0.6-javadoc.jar, and import the package to the lib folder. (Note: if an error occurs, import all the packages in Jdom)

 

Example 1: Use jdom to create an xml file named "people. xml"

Create a class CareateJdom

 

Package com. book. jdom; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import org. jdom2.Document; import org. jdom2.Element; import org. jdom2.output. format; import org. jdom2.output. XMLOutputter; // generate the xml file public class CreateJdom {public static void main (String [] args) {// define the Element people, student; people = new Element ("people"); student = new Element ("st Udent "); // set the property student. setAttribute ("name", "Zhang San"); student. setAttribute ("salary", "8000"); // sets the text student. setText ("Haha"); // Add it to the people. addContent (student); // create a document. Document doc = new Document (people); // read Format, assigned to the current Format format Format = Format. getCompactFormat (); // initialize the current format. setEncoding ("UTF-8"); // set the xml file indent 4 spaces format. setIndent (""); // create an xml output factory and send the format to the factory XMLOutputter xmlout = new XMLOutputter (format); try {// send the written text to the factory, create a file output stream and output the data to xmlout. output (doc, new FileOutputStream ("people. xml "); System. out. println ("successful! ");} Catch (FileNotFoundException e) {// TODO Auto-generated catch block e. printStackTrace ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}/ * running result: <? Xml version = "1.0" encoding = "UTF-8"?> <People> <student name = "Zhang San" salary = "8000"/> </people> **/

 

Example 2: Use Jdom to parse the people. xml file

Create a Readxml class

Package com. book. jdom; import java. io. IOException; import java. util. list; import org. jdom2.Document; import org. jdom2.Element; import org. jdom2.JDOMException; import org. jdom2.input. SAXBuilder; // read people. xml document public class Readxml {public static void main (String [] args) {// create a constructor to parse xml SAXBuilder sax = new SAXBuilder (); // create a Document to accept the Document doc; try {// get people. xml document doc = sax. build ("people. xml "); // get the root node Element people = doc. getRootElement (); // obtain the node data List under the root node <Element> list = people. getChildren (); for (int I = 0; I <list. size (); I ++) {Element e = list. get (I); // obtain the attribute value System. out. println ("name:" + e. getAttributeValue ("name") + "salary:" + e. getAttributeValue ("salary"); // obtain the text value System. out. println (e. getText () ;}} catch (JDOMException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace () ;}}/ ** running result: * name: Michael salary: 8000 Haha **/

 

Parse xml

How to Use jdom to obtain different attribute values with the same tag name <? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <Key Name = "China"> <Value Name = "TextKey"> China </Value> <Value Name = "Enabled"> true </Value> <Value Name = "PhotoIDWidth"> 38 PhotoIDWidth </Value> <Value Name = "PhotoIDHeight"> 38 </Value> <Key Name = "Adult"> <Value Name = "CrownPercent"> 0.10 </Value> <Value Name = "HeadPercent"> 0.60 AdultHeadPercent </Value> </Key> <Key Name = "Child"> <Value Name = "CrownPercent"> 0.10 </Value> <Value Name = "HeadPercent"> 0.60 ChildHeadPercent </Value> </Key> <Key Name = "Australia"> <Value Name = "TextKey "> Australia </Value> <Value Name =" Enabled "> true </Value> <Value Name =" PhotoIDWidth "> 35 PhotoIDWidth </Value> <Value Name =" PhotoIDHeight "> 45 </Value> <Key Name =" Adult "> <Value Name =" CrownPercent "> 0.061 </Value> <Value Name =" HeadPercent "> 0.756" Adult" headPercent </Value> </Key> <Key Name = "Child"> <Value Name = "CrownPercent"> 0.072 </Value> <Value Name = "HeadPercent"> 0.711 ChildHeadPercent </Value> </Key> <Key Name = "Austria"> <Value Name = "TextKey"> Austria </Value> <Value Name = "Enabled"> true </Value> <Value Name = "PhotoIDWidth"> 35 PhotoIDWidth </Value> <Value Name = "PhotoIDHeight"> 45 </Value> <Key Name = "Adult"> <Value Name = "CrownPercent"> 0.064 </Value> <Value Name = "HeadPercent"> 0.744 AdultHeadPercent </Value> </Key> <Key Name = "Child"> <Value Name = "CrownPercent"> 0.078 </Value> <Value Name = "HeadPercent"> 0.689 ChildHeadPercent </Value> </Key> </Configuration> package input; import java. io. IOException; import java. util. arrayList; import java. util. list; import org. jdom. document; import org. jdom. element; import org. jdom. JDOMException; import org. jdom. input. SAXBuilder; public class ReadXML {/*** @ param args */public static void main (String [] args) throws JDOMException, IOException {SAXBuilder sb = new SAXBuilder (); // construct the Document Object Document doc = sb. build (Test. class. getClassLoader (). getResourceAsStream ("nation. xml "); // get the root Element root = doc. getRootElement (); // locate <Configuration>-> <Key> List <Element> list = root. getChildren ("Key"); List <Element> children = new ArrayList <Element> (); List <Element> childrens = new ArrayList <Element> (); for (int I = 0; I <list. size (); I ++) {Element element = (Element) list. get (I); System. out. print (element. getAttributeValue ("Name"); // locate <Configuration>-> <Key>-> <Value> children = element. getChildren ("Value"); for (int j = 0; j <children. size (); j ++) {Element elementChildren = (Element) children. get (j); // locate <Configuration>-> <Key>-> <Value Name = "PhotoIDWidth"> if (elementChildren. getAttributeValue ("Name "). equals ("PhotoIDWidth") {// obtain the <Configuration>-> <Key>-> <Value Name = "PhotoIDWidth"> attribute Value System. out. print ("<--------->" + elementChildren. getAttributeValue ("Name"); // obtain the Content System in the <Configuration>-> <Key>-> <Value Name = "PhotoIDWidth"> tag. out. print ("," + elementChildren. getText () ;}} children. clear (); // locate <Configuration>-> <Key> children = element. getChildren ("Key"); for (int j = 0; j <children. size (); j ++) {Element elementChildren = (Element) children. get (j); // locate <Configuration>-> <Key Name = "Child"> if (elementChildren. getAttributeValue ("Name "). equals ("Child") {// locate <Configuration>-> <Key Name = "Child">-> <Value> childrens = elementChildren. getChildren ("Value"); for (int k = 0; k <childrens. size (); k ++) {Element elementChildrens = (Element) childrens. get (k ); // locate <Configuration>-> <Key Name = "Child">-> <Value Name = "HeadPercent"> if (elementChildrens. getAttributeValue ("Name "). equals ("HeadPercent") {System. out. println ("<--------->" + elementChildrens. getText () ;}}}}} print result: China <---------> PhotoIDWidth, 38 PhotoIDWidth <---------> 0.60 ChildHeadPercentAustralia <---------> PhotoIDWidth, 35 PhotoIDWidth <---------> 0.711 ChildHeadPercentAustria <---------> PhotoIDWidth, 35 PhotoIDWidth <---------> 0.689ChildHeadPercentJdom Parse xml CodeParse 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.