This is the 20th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
1. Introduction to XML
What is XML?
Extensible Markup Language, a subset of the standard Common Markup Language, or XML. Is a markup language for marking electronic files with structure.
In an electronic computer, a marker is a symbol of information that a computer can understand, through which computers can process various types of information such as articles. It can be used to tag data, define data types, and is a source language that allows users to define their own markup language. It is well suited for World Wide Web transport, providing a unified way to describe and exchange structured data independent of applications or vendors. It is a cross-platform, content-dependent technology in the Internet environment and an effective tool for dealing with distributed structured information today. Back in 1998, the W3C published the XML1.0 specification to simplify the transfer of document information over the Internet.
What does XML do?
XML is designed to transfer and store data.
2. XML syntax
- Document declaration.
- Element (tag)
- XML attributes
- XML comments
- Text area (CDATA area)
Note:
- Unlike HTML, all XML elements must have a closing tag (that is, closing)
- XML tags are case-sensitive
- XML must be properly nested
- XML documents must have a unique root element, or top-level element
- XML attribute values must be quoted
- Special characters in XML are represented the same way as in HTML
2.1 Document Declaration
Create an XML file
<! Encoding encoding -->
Copy the code
attribute
- Version indicates the version number
- Encoding is the file encoding of XML
- Standalone =” Yes /no” indicates whether the XML file is a standalone XML text
2.2 Elements (Labels)
2.2.1 What is an XML element?
An XML element is the part that runs from (and includes) the start tag to (and includes) the end tag.
An element can contain other elements, text, or a mixture of the two. Elements can also have attributes. XML elements are similar to HTML tags
<bookstore>
<book category="CHILDREN">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
Copy the code
In the above example, and both have element content because they contain other elements. Only text content, because it contains only text.
In the above example, only elements have attributes (category=”CHILDREN”).
2.2.2 XML Naming Rules
XML elements must follow the following naming rules:
- A name can contain letters, numbers, and other characters
- Names cannot start with numbers or punctuation marks
The name cannot start with the character “XML” (or XML, XML)- The name cannot contain Spaces
Use any name, no reserved words.
2.3 XML attributes
XML elements can contain attributes in the start tag, similar to HTML.
Attributes provide additional information about elements.
Attribute values must be enclosed in quotes
<! Sex is a property -->
<person sex="female">
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
<! -- sex is a child -->
<person>
<sex>female</sex>
<firstname>Anna</firstname>
<lastname>Smith</lastname>
</person>
Copy the code
In the first example, sex is a property. In the second example, sex is a child element. Both examples provide the same information.
2.4 XML comments
HTML and XML annotations are the same:
<! - comments -- >
Copy the code
2.5 Text Area (CDATA Area)
The text content in CDATA is just plain text and will not be parsed by XML syntax
CDATA format:
<! [CDATA]]>Copy the code
Ex. :
<! Encoding encoding -->
<BOOK><! [CDATA]]></BOOK>
Copy the code
3. XML parsing technology – DOM4j ★
3.1 Downloading the DOM4j JAR Package
Download dom4j.github. IO /
Once downloaded, create a lib directory and add the DOM4j JAR package. And added to the classpath. The [dom4j] JAR package is imported
3.2 Dom4J programming steps
- First load the XML file to create the Document object
- Get the root element object from the Document object
- By the root element. Elelemts (tag name); I can return a set, and I can return a set of. All element objects with the tag name you specify
- Find the child element you want to modify or delete, and do the same
[student. XML]
<! Encoding encoding -->
<students>
<student sn="2020020197"> <! -- sn attribute is student number -->
<name>Von neumann</name> <! -- name tag is name -->
<age>20</age> <! -- Age tag = age -->
</student>
<student sn="2020020198"> <! -- sn attribute is student number -->
<name>Turing</name> <! -- name tag is name -->
<age>22</age> <! -- Age tag = age -->
</student>
</students>
Copy the code
【 Student. Java 】
public class Student {
private String sn;
private String name;
private int age;
public Student(String sn, String name, int age) {
this.sn = sn;
this.name = name;
this.age = age;
}
public Student(a) {}@Override
public String toString(a) {
return "Student{" +
"sn='" + sn + '\' ' +
", name='" + name + '\' ' +
", age=" + age +
'} ';
}
public String getSn(a) {
return sn;
}
public void setSn(String sn) {
this.sn = sn;
}
public String getName(a) {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge(a) {
return age;
}
public void setAge(int age) {
this.age = age; }}Copy the code
Parsing XML files
public void getDocument(a) throws DocumentException {
// 1. Create a SAXReader object. To read the XML file and get the Document object
SAXReader reader = new SAXReader();
Document document = reader.read("src/student.xml");
// 2. Pass the Document object. Get the root element object of the XML
Element root = document.getRootElement();
// 3. Pass the root element object. Gets all book tag objects
List<Element> students = root.elements("student");
// 4. Iterate over each student tag object. We then get each element within the Student tag object
for (Element student : students) {
//elementText("name") gets the text content of the name tag
String nameText = student.elementText("name");
//elementText("age") gets the text content of the age tag
String ageText = student.elementText("age");
//attributeValue() gets the text content of the SN attribute
String snValue = student.attributeValue("sn");
// instantiate the Javabean class receive
System.out.println(newStudent(snValue, nameText, Integer.parseInt(ageText))); }}Copy the code
Persistence is the best tool to deal with difficulties and obstacles in the path of dreams. It will help one to cut through difficulties, through difficulties, and reach the flower covered dream