** Preface: a good memory is better than a bad pen, for fragmentary knowledge can best be recorded and summarized, convenient to view later. Otherwise, it is always a waste of time to learn from scratch.

What is XML

XML (eXtensible Markup Language) is mainly used to save data. ,

Similarities and differences between XML and HTML

Similarities: both labels; Differences: XML has no predefined tags, whereas HTML has many predefined tags (note: HTML can also customize tags, click to see how to define them). HTML is used to display information, XML is used to save data, transmit data, etc.

XML is used as a configuration description file for Java programs. XML is used as a configuration description file for Java programs. XML is used as a configuration description file for Java programs.

XML document structure

1. The first line must be an XML declaration

Basic information about an XML document, including the version number and character set, is shown as follows:


      
Copy the code

The version attribute specifies the VERSION of the XML as 1.0. The encoding attribute specifies the encoding of the XML document.

2. There is only one root node
3 The rules for writing tags are the same as those for HTML

4. XML label writing rules

  • Legal tag names use English words separated by hyphens (-),
  • Comment and indent appropriately
  • Fair use attribute
  • Special characters and CDATA tags The use of some characters, such as “<“, “>”, will destroy the document structure, need to use entity reference;
Entity references Corresponding symbols
&lt; <
&gt; >
&amp; &
&apos; ‘single quotes
&quot; “Double quotation marks

If there are a large number of special characters, you can use the CDATA tag to indicate that there is no plain text data in the tag, as follows:

<! [CDATA[ <h1> hello world</h1> ]]>Copy the code
  • Ordered child elements

XML semantic constraints

Semantic constraints define the “semantics” of tags in XML, that is, which tags can appear, which attributes the tags have, which and how many subtags a tag can define, which content of a tag is a subtag or text, and so on. You can think of HTML as officially predefined tags, while XML defines custom tags through semantic constraints. Make XML valid.

There are two kinds of semantic constraints, DTD and Schama

1. DTD semantic constraints
hr.dtd

<! ELEMENThr (employee+) >The hr element contains at least one employee element.<! ELEMENTemployee(name.age.salary.department) >
<! ATTLISTemplayee no CDATA"> 
       Copy the code

Hr (Employee +) contains one or more employee elements. That is, at least one HR (employee*) contains zero or more hr(Employee?) elements. Contains zero or one

How DTD files are referenced in XML documents

<DOCTYPE root SYSTEM “file name, including path “>

hr.xml


      
<! DOCTYPEhr SYSTEM "hr.dtd">
<! -- Human Resource Management System -->
<hr>
    <employee>
        <name>Bob</name>
        <age>18</age>
        <salary>3000</salary>
        <department>
            <dname>dev</dname>
            <address>WuHan</address>
        </department>
    </employee>
</hr>

Copy the code

Local DTD constraints, which you can tell from the declaration that DTD files are also XML


      
<! ELEMENThr (employee*) >
<! ELEMENTemployee (name.age.salary.department) >
<! ATTLISTemployee no CDATA "000">
<! ELEMENTname (#PCDATA) >
<! ELEMENTage (#PCDATA) >
<! ELEMENTsalary (#PCDATA) >
<! ELEMENTdepartment (dname.address) >
<! ELEMENTdname  (#PCDATA) >
<! ELEMENTaddress (#PCDATA) >
Copy the code
2. Schema constraint