There are four types of parsing xml files in java. DOM, SAX, DOM4J, and JDOM. In my first article, I will introduce how to use Jdom to parse XML. I think one of these four learning methods is enough. The other three parsing ideas are not much different. In addition, you can query the advantages and disadvantages of these four introductions online. The following is an example I wrote. I can understand the example carefully.
Test java:
Package com. rthb. test;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. util. List;
Import org. jdom. Document;
Import org. jdom. Element;
Import org. jdom. JDOMException;
Import org. jdom. input. SAXBuilder;
Import org. jdom. output. XMLOutputter;
Public class TestXml {
/**
* Created by: zhanglx
* Creation Time: 11:28:19 AM
* Description: used to read an xml file.
* @ Throws IOException
* @ Throws JDOMException
* @ Throws IOException
* @ Throws JDOMException
*/
@ SuppressWarnings ("unchecked") www.2cto.com
Public static void main (String [] args) throws JDOMException, IOException {
// ReadXml (); // read the xml file
// AddXml (); // Add xml Information
// DeleteXml ("Shanghai press"); // Delete the book node genre = "Shanghai press"
UpdateXml ("People's Publishing House"); // modify the book node genre = "People's Publishing House"
}
// Read the xml file
@ SuppressWarnings ("unchecked ")
Public static void ReadXml () throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder (); // instance JDOM parser
Document document = builder. build ("src/bookstore. xml"); // read the xml file
Element root = document. getRootElement (); // obtain the root node <bookstore>
List <Element> list = root. getChildren (); // obtain the child node of the root node.
For (Element e: list ){
System. out. println ("Press:" + e. getAttributeValue ("genre "));
System. out. println ("Security Code:" + e. getAttributeValue ("ISBN "));
System. out. println ("title:" + e. getChildText ("title "));
System. out. println ("author:" + e. getChildText ("author "));
System. out. println ("price:" + e. getChildText ("price "));
System. out. println ("========================================== ===== ");
}
}
// Add xml file information (that is, add a node information to xml)
@ SuppressWarnings ("unchecked ")
Public static void AddXml () throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder (); // instance JDOM parser
Document document = builder. build ("src/bookstore. xml"); // read the xml file
Element root = document. getRootElement (); // obtain the root node <bookstore>
Element element = new Element ("book"); // Add a new node <book> </book>
Element. setAttribute ("genre", "Shanghai press"); // Add the genre attribute to the book Node
Element. setAttribute ("ISBN", "6-3631-4"); // Add the ISBN attribute to the book node.
Element element1 = new Element ("title ");
Element1.setText ("sorrow flow into the river ");
Element element2 = new Element ("author ");
Element2.setText ("guo jingming ");
Element element3 = new Element ("price ");
Element3.setText ("32.00 ");
Element. addContent (element1 );
Element. addContent (element2 );
Element. addContent (element3 );
Root. addContent (element );
Document. setRootElement (root );
// File Processing
XMLOutputter out = new XMLOutputter ();
Out. output (document, new FileOutputStream ("src/bookstore. xml "));
}
// Delete xml Information (that is, delete an xml node)
@ SuppressWarnings ("unchecked ")
Public static void DeleteXml (String str) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder (); // instance JDOM parser
Document document = builder. build ("src/bookstore. xml"); // read the xml file
Element root = document. getRootElement (); // obtain the root node <bookstore>
List <Element> list = root. getChildren (); // obtain the subnodes of all root nodes <bookstore> <book>
For (Element e: list ){
// Delete the book node genre = "Shanghai Publishing House"
If (str. equals (e. getAttributeValue ("genre "))){
Root. removeContent (e );
System. out. println ("deleted successfully !!! ");
Break;
}
}
// File Processing
XMLOutputter out = new XMLOutputter ();
Out. output (document, new FileOutputStream ("src/bookstore. xml "));
}
// Modify the xml file
@ SuppressWarnings ("unchecked ")
Public static void UpdateXml (String str) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder (); // instance JDOM parser
Document document = builder. build ("src/bookstore. xml"); // read the xml file
Element root = document. getRootElement (); // obtain the root node <bookstore>
List <Element> list = root. getChildren (); // obtain the subnodes of all root nodes <bookstore> <book>
For (Element e: list ){
// Delete the book node genre = "Shanghai Publishing House"
If (str. equals (e. getAttributeValue ("genre "))){
E. getChild ("title"). setText ("111111111 ");
System. out. println ("modification successful !!! ");
Break;
}
}
// File Processing
XMLOutputter out = new XMLOutputter ();
Out. output (document, new FileOutputStream ("src/bookstore. xml "));
}
}
Test xml: bookstore. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<Bookstore>
<Book genre = "Jiangnan Publishing House" ISBN = "1-3631-4">
<Title> tomb stealing notes </title>
<Author> uncle nanpi </author>
<Price> 25.00 </price>
</Book>
<Book genre = "Yanbian press" ISBN = "2-3631-4">
<Title> three secrets </title>
<Author> Han </author>
<Price> 35.00 </price>
</Book>
<Book genre = "China Publishing House" ISBN = "3-3631-4">
<Title> ordinary world </title>
<Author> Lu Yao </author>
<Price> 27.00 </price>
</Book>
<Book genre = "Hubei press" ISBN = "4-3631-4">
<Title> Security on demand </title>
<Author> Meng Fei </author>
<Price> 30.00 </price>
</Book>
<Book genre = "People's Publishing House" ISBN = "5-3631-4">
<Title> 111111111 </title>
<Author> Yu Qiuyu </author>
<Price> 40.00 </price>
</Book>
</Bookstore>
Use: jdom-1.0.jar pack
Conclusion: when reading an xml file. Do you think it is a bit like reading a file with an io stream? In fact, the xml file is read by the file. However, the file is xml. I have some questions about the internal characteristics of xml files, so I need to read them one by one. For example, read the root node, then cycle the child nodes inside the root node, and finally read the values of attributes and elements in each child node cyclically.
This is also true when adding an xml file. First, read the file and find the root node. Then, add the child nodes in Shenyang and what attributes and elements should be added to the child nodes.
In fact, after the above operations, do you not think that xml files can be used as a small database? Haha... I think it should be okay. He can add, delete, modify, and query the content, so he should be able to use it as a small database.