1, Introduction to XML
-
preface:
XML is an Extensible Markup Language, a subset of the standard general markup language. It is a markup language used to mark electronic documents and make them structural. -
What is XML?
XML is an extensible markup language, similar to HTML, which is designed to transmit data rather than display data. XML tags are not predefined. You need to define your own tags. XML is designed to be self descriptive and is a W3C recommendation. -
What is the role of xml?
1,It is used to save data, and the data is self descriptive 2,You can also make configuration files for its projects or modules 3,It can also be used as the format of network transmission data (now JSON Mainly).
xml file example:
<?xml version="1.0" encoding="utf-8" ?> <!-- xml statement version It means version encoding It's coding standalone="yes/no" Means this xml Is the file independent xml file --> <books> <!-- This is xml notes --> <book id="SN123123413241"> <!-- book The label describes a book id Attribute describes the number of the book --> <name>java Programming thought</name> <!-- name The label describes the information of the book --> <author>Huazi</author> <!-- author The word means the author and describes the author of the book --> <price>9.9</price> <!-- price The word is the price and describes the price of books --> </book> <book id="SN12341235123"> <!-- book The label describes a book id Attribute describes the number of the book --> <name>Sunflower collection</name> <!-- name The label describes the information of the book --> <author>monitor</author> <!-- author Words are the meaning of the author and describe the author of the book --> <price>5.5</price> <!-- price The word is the price and describes the price of books --> </book> </books>
2, XML syntax
2.1 xml comments
html and XML Same note : <!-- html notes -->
2.2 elements (labels)
-
What is an XML element
Element refers to the content from the start tag to the end tag. For example:<title>java Programming thought</title>
-
XML naming rules
XML elements must follow the following naming rules:
Names can contain letters, numbers, and other characters The name cannot start with a number or punctuation mark The name cannot be in characters“ xml"(perhaps XML,Xml)start The name cannot contain spaces
-
Elements (tags) in xml are also composed of single tags and double Tags:
Single label Format: <Tag name attribute="Value attribute="Value " ...... /> Double label Format:< Tag name attribute="Value attribute="Value " ......>Text data or sub labels</Tag name>
2.3 xml attributes
Attributes must be enclosed in quotation marks. If they are not quoted, an error will be reported
2.4 grammar rules
-
All XML elements must have a close tag (that is, close)
-
XML tags are case sensitive
<Message>This is wrong.</message> <message>This is correct.</message>
-
XML must be nested correctly
-
The XML document must have a root element
The root element is the top element,
Elements without parent tags are called top-level elements.
The root element is a top-level element without a parent tag and is the only one.
-
XML attribute values must be quoted
-
Special characters in XML
-
Text area (CDATA area)
CDATA syntax can tell the xml parser that the text content in CDATA is only plain text and does not need xml syntax parsingCDATA Format: <![CDATA[ Here you can display the characters you entered as they are without parsing xml ]]>
3, Introduction to xml parsing technology
-
xml parsing is to transform xml documents into xml DOM objects
-
The early JDK provided us with an introduction to two xml parsing technologies DOM and Sax (outdated, but we need to know these two technologies)
-
dom parsing technology is developed by W3C organization, and all programming languages implement this parsing technology using the characteristics of their own language. Java also implements dom technology parsing tags.
-
sun company upgraded dom parsing technology in JDK5 version: SAX (Simple API for XML)
SAX parsing, which is different from the parsing formulated by W3C. It uses a similar event mechanism to tell the user the content being parsed through callback. It reads and parses the xml file line by line. A large number of DOM objects will not be created. Therefore, when parsing xml, it is in the use of memory. And performance. Are better than DOM parsing.
Third party parsing technology:
jdom stay dom On this basis, the packaging dom4j Right again jdom Encapsulated. (key points) pull Mainly used in Android Mobile phone development is with sax Very similar. They are all event mechanism parsing xml Documents.
4, dom4j parsing technology (key)
Since dom4j is not the technology of sun company, but the technology of a third-party company, we need to download the jar package of dom4j from the official website of dom4j if we need to use dom4j.
dom4j programming steps:
Step 1: add dom4j of jar Bag. And add to the classpath. Step 2: load first xml File creation Document object Step 3: Pass Document Object gets the root element object Step 4: pass the root element.elelemts(Tag name); You can return a set, which is placed in the set. All element objects with the tag name you specify Step 5: find the child element you want to modify or delete and carry out the corresponding operation
Code example:
@Test public void test2() throws Exception{ //1. Create a saxReader input stream to read xml configuration files and generate Document objects SAXReader reader = new SAXReader(); Document document = reader.read("src/books.xml"); //2. Get the following elements through the Document object Element rootElement = document.getRootElement(); //3. Get the book tag object by following the element //Both element() and elements() find child elements by tag names List<Element> books = rootElement.elements();// Element. Elements (tag name), which can get the set of specified child elements under the current element //4. Traverse and process the conversion of each book tag into a Book class for(Element book : books){ //axXML() converts the label object to a label string Element nameElement = book.element("name"); //The getText() method gets the text content between the start tag and the end tag String nameText = nameElement.getText(); //Get the text content of the specified tag name directly String priceText = book.elementText("price"); String authorText = book.elementText("author"); //Gets the property value of the label object String snValue = book.attributeValue("sn"); System.out.println(new Book(snValue,nameText,Double.parseDouble(priceText),authorText)); } }
Books that need to be resolved XML file content:
<?xml version="1.0" encoding="UTF-8"?> <books> <book sn="SN12341232"> <name>Evil dispelling sword manual</name> <price>9.9</price> <author>headmaster</author> </book> <book sn="SN12341231"> <name>Sunflower collection</name> <price>99.99</price> <author>monitor</author> </book> </books>