Next, this article describes the parsing of XML documents using Jdom,
First of all, we should still know where jdom from, is Breet McLaughlin and Jason Hunter two major Java masters of the creative achievements, early 2000,
Jdom officially started development as an open source project. Jdom is a Java toolkit that parses XML.
Jdom is based on the tree structure and uses pure Java technology to parse, generate, serialize and manipulate XML documents.
We start with the following steps:
(1), create an Saxbuilder object through Jdom API
Saxbuilder Saxbuilder = new Saxbuilder ();
(2), create an input stream, load the XML file into the input stream
InputStream in = new FileInputStream ("Src/res/books.xml");
(3), through the Saxbuilder Builder () method, the input stream is loaded into the Saxbuilder, returning a reference to a Document object,
Document document = Saxbuilder.build (in);
We then get the root node of the XML through the Document object.
Element rootelement = Document.getrootelement ();
Next we operate on the child nodes of the XML, getting the root node sub-node list collection.
List<element> Booklist = Rootelement.getchildren ();
Iterates over the collection Booklist, gets the name of the child node and the value of the child node,
for(Element book:booklist) {List<Attribute> attrlist =book.getattributes (); for(Attribute attr:attrlist) {//Get Property nameString Attrname =Attr.getname (); //Get Property valueString AttrValue =Attr.getvalue (); System. out. println ("Property Name:"+ Attrname +"----Property Value:"+attrValue); } //The node name of the child node of the book node and the traversal of the node valueList<element> Bookchilds =Book.getchildren (); for(Element child:bookchilds) {System. out. println ("node Name:"+ child.getname () +"----Node Value:"+Child.getvalue ()); } }
Here are the Books.xml
<?XML version= "1.0" encoding= "UTF-8"?><Bookstore> < BookID= "1"> <name>The old man and the sea</name> <author>Hemingway</author> < Year>1955</ Year> < Price>45</ Price> </ Book> < BookID= "2"> <name>The book sword and the Hatred record</name> < Year>1959</ Year> < Price>24</ Price> <language>Chinese</language> </ Book> </Bookstore>
The result of the output is:
Property name: id----Attribute value: 1 node Name: name---- node value: Old Man and Sea node name: Author---- node value: Hemingway Day Name:year----node value: 1955 Section name:Price----node Value: Two property name: ID----attribute value: 2 node Name: name---- Node value: The book sword and the Record node name:Year----node value: 1959 node Name:Price----Node value: node name: Language----node value: Chinese
In this way our simplest jdom parsing xml file is done, summarizing the pros and cons of a jdom.
Advantages: ① is a tree-based Java API that handles XML and loads the tree in memory
② has no backward-compatible limits and is therefore simpler than DOM
③ speed, low defect
④ Java rules with sax
Disadvantage: ① cannot handle documents larger than memory
②jdom represents the XML document Logical model. There is no guarantee that each byte really transforms.
③ does not provide any actual model of DTDs and schemas for instance documents.
④ does not support the corresponding traversal of the package with the DOM
Best for: Jdom has the convenience of a tree, there are also Java rules for Sax. Use when balance is required.
Talking about parsing XML document with Java (III.)