XML is an extensible markup language (eXtensible Markup Language), the file extension .xml
Uses: description, data transmission
Use:
<?xml version="1.0" encoding="UTF-8" ?>
<! - comment format, beginning of the XML declaration ->
<! - There must be only one root element, case sensitive tag ->
<Students>
<-! [] Attribute value in double quotes ->
<Student id="2002">
<! - tag pairs, and elements to be properly nested, nested allowed cross ->
<name>Joe Smith</name>
<age>28</age>
<class>12 classes</class>
<! - label-free body may be abbreviated ->
<dcr/>
</Student>
<Student id="2003">
<name>John Doe</name>
<age>25</age>
<class>14 classes</class>
</Student>
</Students>
An XML element can contain letters, numbers and some other visible character, but must comply with some of the following specifications:
Analytical methods for the XML file:
DOM parsing (Document Object Model) Document Object Model
(1) ease of use and strong, the use of DOM, XML documents will all information is stored in memory and simple traversal, supports XPath, enhanced ease of use
(2) inefficient, slow parsing, the memory footprint is too high, almost impossible to use for larger files,
(3) supported CRUD
SAX parsing (Simple API for Xml)
(1) to "push" event-driven model XML processing, although it is not W3C standards, but it is a widely accepted API
(2) The biggest advantage is the small memory consumption
(3) only for reading
Dom4j is a simple, flexible open source library. Dom4j is a very good Java XML API, with excellent performance, powerful and extremely easy to use features.
Dom4j use development, need to download the appropriate Dom4j jar file.
How to use the project DOM4J
IDEA configuration:
create (1) a project folder lib
(2) copy the packet into the jar lib directory
(3) right-click on the folder lib -> Add as Library
Eclipse configuration:
create (1) a project folder lib
(2) copy the packet into the jar lib directory
(3) Right-click the jar package -> build path-> add to build path
Read and write operations to change deleted
DOM is a tree structure, XML above example, the root node is Students., Which is a leaf node Student, attribute child node is a child, it is also from the resolution process begins with the node, gradually resolved.
// Create SAXReader class object, then use the read method to read
SAXReader reader = new SAXReader();
Document document= reader.read(new File("input.xml"));
// Get the root
Element root = document.getRootElement();
// add an element under the root
Element book = root.addElement("book");
// add the book value of the [property]
book.addAttribute("id", "1008");
// add a child node child
book.addElement("name").setText("" Jiuyangzhenjing "");
book.addElement("author").setText("Zhang Wuji");
book.addElement("price").setText("89.9");
// add the result still in memory, there needs to be written to a file
1 // Create output format
OutputFormat outputFormat = OutputFormat.createPrettyPrint();
outputFormat.setEncoding("utf-8");
// 2. Create a file format XMLWriter
XMLWriter writer = new XMLWriter(new FileWriter("input.xml"), outputFormat);
// 3. Write and Close
writer.write(document);
writer.close();
1 // Create SaxReader
SAXReader reader = new SAXReader();
2 // Read
Document document = reader.read(new FileReader("input.xml"));
3 // Get the root
Element root = document.getRootElement();
// 4. Modify elements
List<Element> bookList = root.elements("book");
Element firstBook = bookList.get(0);
firstBook.element("price").setText("101");
Element secondBook = bookList.get(1);
secondBook.element("price").setText("120");
// 5. Delete the last
root.remove(bookList.get(bookList.size() - 1));
// 6. write file
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(new FileWriter("input.xml"), format);
// 7. Write
writer.write(document);
// 8. Close
writer.close();
public static void readxml() throws Exception{
1 // create SaxReader
SAXReader reader=new SAXReader();
2 // Gets the Document object
Document document=reader.read(new FileReader("src\\books2.xml"));
// get the root node 3
Element root=document.getRootElement();//books
//System.out.println(root.getName());
4 // get book collection, traversing get
List<Element> bookList=root.elements("book");
for (Element b : bookList) {
// get 5 [property value]
String id=b.attributeValue("id");
// 6. obtaining sub-sub-node
String name=b.element("name").getText();
String author=b.element("author").getText();
String price=b.elementText("price");
Book book=new Book(Integer.parseInt(id), name, author, Double.parseDouble(price));
System.out.println(book.toString());
}
}
The basic operation is the read SAXReader file, obtaining the root node, then the child element method to obtain child node and child nodes, etc., and finally write the file using XMLWriter
Failed to parse XML file problemXML parsing failed, mainly because of problems caused by BOM header, seeFlow and the buffer flow conversion system using the - basic use
The solution is to use notepad ++ open, and then select "Use UTF-8 encoding", save it in the above "coding" option.
Recently, I was looking for a job and saw that many companies' requirements stated that XML parsing was required, so I reviewed the previous XML parsing knowledge and wrote a small example. Several co...
If you use PHP to parse XML, the common choices are as follows:DOM、SimpleXML、XMLReader. If you want to parse a large XML file, the first thing to exclude is DOM, because when using DOM, you need to lo...
First, download a JDOM jar package, choose one of the following two websites, select the appropriate version and download it and import the project Download URL of jar package: http://mvnrepository.co...
First, my XML file is named a.xml, and the path is in the project directory.The content is as follows: The implementation code is as follows: The effect is as follows:...
I. Introduction There are many articles using xstream to parse xml files on the Internet, and I have referenced a lot of them. First of all, I would like to express my gratitude to these apes who sile...
Create and read the following xml file: Code:...
The operation of the Pull parser is similar to the SAX parser. It provides similar events, such as: start element and end element events, use parser.ne...
Add JAR dependence in POM.xml using the IDEA: If you only add DOM4J, run the code error when you don't add JAXEN. java.lang.NoClassDefFoundError: org/jaxen/JaxenException ZANG.XML content: operation r...
XML file: Book entity class: Test class: Output results:...
1 Introduction Reading and setting an XML profile is the most commonly used operation, TinyXML is an open source parsing XML C ++ resolution library, which can be compiled in Windows or Linux. The mod...