[Java] Java Core 72: XML (Part 1)



insert image description here

1 Overview of XML

What is XML

  1. English: Xtensible Markup LanguageExtensible markup language, composed of various tags (labels, elements).
  2. Extensible: All labels are customizable and can be extended at will. Such as: <abc/>, <name>
  3. Markup Language: The entire document is composed of various tags. Clear, data structured!
  4. XML is a universal format standard. All technicians in the world know this thing, and they will store and interact with data according to XML specifications! !

XML role

Function: Generally speaking, it is for storing and maintaining data.

  1. Data exchange: data exchange between different computer languages, different operating systems, and different databases.
    insert image description here

  1. Configuration files: In the later stage, we mainly see configuration files for various frameworks every day.

For example, we will soon learn about the connection pool: c3p0-config.xml

insert image description here


2 Write the first XML file

need

Write an xml document to describe personnel information, person represents a person, and id is the attribute of the person representing the number of the person. Personnel information includes age, name, and sex.

Use Java classes to describe:

class Person{
    
    
  String id;
  int age;
  String name;
  String sex;
}

Person p = new Person("1","张三",18,"男");

Effect

insert image description here

step

  1. Select the current project with the right mouse button to create a new one

    When naming a new file, it should end with .xml. This file is the xml file

insert image description here

  1. Write person.xml file

    <?xml version="1.0" encoding="UTF-8" ?>
    <peopel>
        <person>
            <id>1</id>
            <name>张三</name>
            <age>18</age>
            <sex></sex>
        </person>
        
        <person>
            <id>2</id>
            <name>李四</name>
            <age>20</age>
            <sex></sex>
        </person>
    
    </peopel>
    
  2. Parse the content of XML through the browser

  • Note: XML will be parsed by Java in the future, and it is rarely displayed directly on the browser.

3 The composition of XML: declarations and elements

XML composition

  1. statement
  2. element (label)
  3. Attributes
  4. note
  5. Escape character [entity character]
  6. CDATA character area

document statement

<?xml version="1.0" encoding="utf-8" ?>    固定格式
  1. IDEA will automatically prompt.

  2. The document declaration must start with <?xml and end with ? > end

  3. The document statement must start from the position of 1 line and 1 column of the document,Must be the first row and first column in the xml document

insert image description here

  1. Two attributes commonly found in document declarations:

    • version: Specifies the XML document version. Required attribute, generally choose 1.0 here;
    • encoding: specifies the encoding of the current document, an optional attribute, and the default value is utf-8;

Elements (labels, tags)

格式1:  <person> 标签体 </person>   有标签体的标签
格式2:  <person/>  没有标签体的标签 (空标签)
  1. Elements are the most important part of an XML document;

  2. The structure of a common element consists of a start tag, an element body, and an end tag. [Format 1]

  3. Element body: The element body can be an element or text , for example:

    <person> 
      标签中可以包含另一个标签  
      <name>张三</name>  
    </person>
    
  4. Empty elements: Empty elements have only tags, but no end tags, but the element must be closed by itself, for example:

    <sex/>
    
  5. element naming

    • case sensitive
    • no spaces, no colons
    • It is not recommended to start with XML, xml, Xml
    • The tag name cannot start with a number, it can have a number
    • Underscores can be used

    Can keep the same rules as Java named identifiers

  6. A well-formed XML document has one and only one root element.

Error demo:

element does not end

insert image description here

Inconsistent uppercase and lowercase elements

insert image description here

multiple roots in xml

insert image description here


4 The composition of XML: attributes, comments and escape characters

attribute syntax

<person id="110">
  1. Attributes are part of an element and must appear in the element's opening tag

  2. Attribute definition format: 属性名=“属性值”, where the attribute value must be enclosed in single or double quotes

  3. An element can have 0~N attributes, but an attribute with the same name cannot appear in an element
    insert image description here

  4. Attribute names cannot use spaces, it is recommended not to use special characters such as colons, and must start with a letter

    It is recommended to use Java's identifier definition rules as a reference

<person id="123">
	<name>张三</name>
</person>

note

<!-- 注释内容 -->

<!-- 
注释内容 1
注释内容 2
-->

XML comments are the same as HTML, both <!--start and -->end. Cannot be nested.

Notes in Java:

// 单行
/* */ 多行注释
/** */ 文档注释

XML comments:

<!-- 注释内容 -->
<!--<person>注释</person>-->  <!-- 快捷键:Ctrl+/ :可以将整行进行注释-->
<person>三生三世</person> <!-- 快捷键:Ctrl+Shift+/:局部注释-->

escape character [entity character]

Entity characters in XML are the same as in HTML. Because many symbols are already used by the document structure, you must use entity characters if you want to use these symbols in element bodies or attribute values

[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-gklSSWwR-1687767096552)(imgs/1552353386585-1602337253871.png)]

character predefined escape characters illustrate
< &lt; less than (less than)
> &gt; greater than
" &quot; double quotes
&apos; single quotes (apostrophe)
& &amp; Ampersand (ampersand )

Note: Strictly speaking, the only characters "<" and "&" are illegal in XML. Ellipses, quotes, and greater than signs are legal, but it's good practice to replace them with entity references.

Example of escape character application:

​ If you put a character like "<" in an XML document, the document will generate an error because the parser will interpret it as the start of a new element. Therefore you cannot write:

<message>if salary < 1000 then </message>

To avoid such errors, you need to replace the character "<" with an entity reference, like this:

<message>if salary &lt; 1000 then</message>

summary

  1. Where does the attribute have to appear in the tag?

    • Bound with the start tag, written behind the start tag element

    • <person id="属性值">
      </person>
      
  2. Can the same tag have attributes with the same name?

    • impossible.
    • Multiple attributes are allowed, separated by spaces, but attributes with the same name cannot appear
  3. Why are there escape characters (entity characters)?

    • In the xml file, some specific symbols are already used by xml. Example: > & etc.
    • If you want to use specific symbols in xml documents, you need to use: escape characters
      • < => &lt;
      • & => &amp;
  4. A quick way to comment?

    • ctrl + /


insert image description here

Guess you like

Origin blog.csdn.net/m0_60915009/article/details/131399941