Summary and use of XML's Dom4j

1. XML (Extensible Markup Language)

  1. X (extensible: extensible)
  2. M (markup mark/label)
  3. L (language language)

HTML language: is the beautiful display data of web pages

The role of XML language:

1: Save data file (single player game)

(Because it is very slow to read the file data saved in the file)

(The database is too large to be saved in the database)

2: Network upload data (large network upload xml file, the game is a package)

3: When the configuration file *.properties, *.xml (ten files, nine xml and one properties)

2. Features of XML

  1. Platform independence (windows, linux), independent language
  2. 90% of languages ​​support xml, and 10% of languages ​​that do not support it are released when xml has not yet been born.
  3. xml is self-describing (content customization)
  4. The difference between hmtl and xml is: html is the tag element officially defined in the xml file, all elements are custom! ! !
<?xml version="1.0" encoding="UTF-8">

3. The grammar rules of XML

  1. xml file must have root element (trunk)
  2. 2.xml elements (tags) must be opened and closed (<p></p>)
  3. 3.xml elements are case sensitive (case sensitive)
  4. 4. Must be properly nested <div><p></p></div> (not <div><p></div></p>)
  5. 5.xml attributes must be added with "" (double quotation marks)

4.CDATA area

When writing special symbols in xml, an error is reported, its nature is ignored, and it becomes a normal string

<p>   <![CDATA[ 10<5 ]]>  </p>

5. DTD file

  1. DTD's English Document Type Definition Document Type Definition
  2. The purpose of DTD: to help you write legal code (self-understanding as similar to abstract class), is a custom specification
  3. Relationship between DTT and XML:
  4. class (human) and object (me) relationship
  5. Relationship between data table table and row (one record)

6. XSD file (is the xml structure definition)

  1. xsd is the xml structure definition (XML Schemas Definition)
  2. xsd is a replacement for dtd, higher end than dtd (you can define your own data types)
  3. The advantages of xsd: (1) the code of xsd is based on xml, there is no special syntax, and the same parsing and processing as xml, (2) xsd supports a series of data types

7. Learn to parse XML

There are four ways to parse xml,

The first two belong to the basic method, which is the platform-independent analysis method provided by the official;

The latter two belong to extension methods, they are extended on top of the basic methods. Only for java platform

  1. DOM parsing (not easy to use)
  2. SAX parsing (not easy to use, no random storage, no random access)
  3. JDOM parsing (not easy to use)
  4. DOM4J parsing (easy to use)

DOM parsing:

  1. The principle of Dom parsing When parsing xml, all elements in the document are based on the hierarchical relationship they appear.

Construct a tree structure in memory,

  1. The advantage of Dom is that it can traverse and modify the content of nodes
  2. The disadvantage is that the memory pressure is large and the parsing is slow

SAX parsing:

  1. is an alternative to xml parsing
  2. Compared with the dom method, sax is a faster and more efficient method
  3. Cannot modify node content

JDOM parsing:

  1. Applies only to concrete classes, not interfaces, not flexible (rarely used)

DOM4J parsing: (emphasis)

  1. An intelligent fork of JDOM that incorporates many features beyond basic XML documents
  2. The well-known underlying framework hibernate uses dom4j to parse.

DOM4J has the highest performance, followed by SAX, DOM and JDOM that perform poorly (memory overflows when parsing 10M XML.)

A case written using dom4j:

8.Xpath (XML file path path)

  1. Xpath is a way to quickly find information in xml documents (is a technology)
  2. When simply using dom4j to access nodes, it needs to be processed layer by layer. If you have Xpath, it is simple to access the nodes of the hierarchy.
  3. Using Xpath requires importing Jaxen.1-1.-bata.jar

Guess you like