XML profile

What is XML? XML stands for EXtensible Markup Language. XML is a markup language much like HTML. XML is designed to transmit data, not display it. XML tags are not predefined. You need to define your own tags. XML is designed to be self-descriptive.

Differences between XML and HTML XML is designed to transfer and store data, with the focus on the content of the data. XML doesn’t do anything HTML is designed to display data, and the focus is on what the data looks like. HTML is designed to display information, while XML is designed to transmit information.

What does XML do? Three functions: 1. It can be used to save data. 2. It can be used as a project or module configuration file. 3, it can be used as a network data transmission format.

XML syntax 1. Document declaration. XML attributes 4. XML comments 5. Text area (CDATA area)

1. Document declaration.

Create an XML file

<? The XML version = “1.0” encoding = “utf-8”? > XML declaration.

and this

The version attribute is the version number. Encoding Is the file encoding of the XML. Standalone = “Yes /no” indicates whether the XML file is an independent XML file

A book has an ID attribute that gives it a unique identifier, a title, an author, a price

<? The XML version = "1.0" encoding = "utf-8"? > <! Encoding --> <books> <! <book id="SN123123413241"> <! <name> </name> <! --> <author> </author> <! <price>9.9</price> <! </book> <book id="SN12341235123"> <! -- the book tag describes a book id attribute describes the book id --> <name> </name> <! The name tag describes the information about the book --> <author> Monitor </author> <! <price>5.5</price> <! --> </book> </books>Copy the code

XML comments

HTML and XML comments are the same: <! — HTML comment –>

Element (tag)

What is an XML element?

The element is the content from the start tag to the end tag.

For example: <title> Java programming ideas

Elements can simply be understood as tags. Element translation

XML elements must follow the following naming rules: 2.1) Names can contain letters, numbers, and other characters such as:

<book id="SN213412341"> <! <author> </author> <! <name> Java programming ideas </name> <! <price>9.9</price> <! --> </book>Copy the code

Names cannot start with numbers or punctuation marks

The name cannot contain Spaces

Elements (tags) in XML are also divided into single and double tags:

A single tag

Format: < Tag name Attribute = “value” attribute = “value”… />

Double label

Format: < Tag name Attribute = “value” attribute = “value”… > Text data or subtags </ tag name >

Grammar rules:

All XML elements must have a close tag (that is, close)

XML tags are case-sensitive

XML must be properly nested

XML documents must have a root element

XML attribute values must be quoted

Special characters in XML

Text area (CDATA area)

CDATA format:

<! [CDATA]>

Introduction to XML parsing technology

XML extensible markup language.

Both HTML and XML files are markup documents that can be parsed using DOM technology developed by the W3C organization.

The Document object represents the entire document (either HTML or XML)

The early JDK provided us with two XML parsing technologies: Dom and Sax introduction (dated, but we need to know this one)

Dom parsing technology is developed by the W3C, and all programming languages use their own language features to implement this parsing technology. Java also implements DOM technology for parsing tags.

In THE JDK5 version, Sun has updated the DOM parsing technology: SAX (Simple API for XML) SAX parsing, which is not quite the same as the parsing formulated by W3C. It uses an event-like mechanism to tell the user what is currently being parsed through callbacks. It reads the XML file line by line for parsing. You don’t create a lot of DOM objects. So it’s in terms of memory usage when parsing XML. And performance. Are better than Dom parsing.

Third party parsing: JDOM is encapsulated on top of DOM, and DOM4J encapsulates JDOM. Pull is mainly used in Android phone development and is very similar to SAX in that it is an event mechanism to parse XML files.

This Dom4j is a third party parsing technology. We need to use a good class library provided by a third party to parse XML files.

Dom4j parsing technology

Since Dom4j is not sun’s technology, but a third-party company’s technology, we need to use Dom4j to download the DOM4j JAR package from dom4j official website.

Use of Dom4j class libraries

Copy the dom4J-1.6.1. jar class package into the project lib directory

The JAR package is then added to the current CLASspath path

Class library added successfully:

Dom4j directory introduction:

Docs is a catalogue of documents

How do I look up Dom4j documentation

Quick introduction to Dom4j

Lib directory

The SRC directory is the source directory for the third party library:



Dom4j programming steps:

Step 1: Create the Document object by loading the XML file

Step 2: Get the root element object from the Document object

Step 3: Pass 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

Step 4: Find the child element you want to modify and delete, and perform the corresponding operations

Step 5, save to hard drive

Gets the contents of the books.xml file that the Document object needs to parse

<? The XML version = "1.0" encoding = "utf-8"? > <books> <book sn="SN12341232"> <name> <price>9.9</price> <author> </author> </book </author> </author> </author> </author> </book> </books>Copy the code

The first step in parsing the code that gets the Document object is to create a SaxReader object. This object is used to read XML files and create documents

Public void getDocument() throws DocumentException {// To create a Document object, We need to create a SAXReader object SAXReader = new SAXReader(); // This object is used to read the XML file and return a Document. Document document = reader.read("src/books.xml"); // Print to console to see if system.out.println (document) was created successfully; }Copy the code

Walking through the tag to get the contents of all the tags (***** emphasis) requires four steps: First, by creating a SAXReader object. To read the XML file and get the Document object and the second step is to go through the Document object. Get the XML root element object and the third step is through the root element object. Gets the fourth smallest of all book label objects, iterating through each book label object. We then get each element of the book tag object and get the text content between the start and end tags using the getText() method

/* * Read the contents of an XML file */ @test public void readXML() throws DocumentException {// First, create a SAXReader object. To read the XML file and get the Document object // Step 2, through the Document object. Get the XML root object // Step 3, pass the root element object. Get all book label objects // the fourth small, iterate over each book label object. We then get each element in the book tag object, and get the text between the start and end tags using the getText() method. SAXReader = new SAXReader(); Document document = reader.read("src/books.xml"); // The second step is through the Document object. Element root = document.getrootelement (); // Print test // element.asxml (), which converts the current Element to a String object // system.out.println (root.asxml ()); // The third step is through the root element object. List<Element> books = root.elements("book"); // The fourth little iterates through each book tag object. For (Element book: books) {// test // system.out.println (book.asxml ()); Element nameElement = book.Element ("name"); Element priceElement = book.Element ("price"); AuthorElement = book. Element ("author"); System.out.println(" name "+ nameelement.gettext () +", Price :" + priceElement. GetText () + ", author :" + rules. GetText ()); }}Copy the code

Print content:



Common mistakes:

No exception was found in the file