[Java] Java Core 73: XML (Chinese)



insert image description here

5 Composition of XML: character area (understand)

When a large number of escape characters appear in the xml document, the readability of the XML document will be greatly reduced. At this time, it would be better to use the CDATA segment.

CDATA (Character Data) character data area, the format is as follows:

<![CDATA[
	文本数据   < >  & ; " "
]]>
  1. CDATA refers to text data that should not be parsed by an XML parser (Unparsed Character Data)
  2. CDATA sections <![CDATA[begin with and ]]>end with ;

For example:

<![CDATA[
    if salary < 1000 then
]]

Shortcut template: CD Enter

insert image description here

Notice:

A CDATA section cannot contain the string "]]>". Nested CDATA sections are also not allowed.

​ The "]]>" marking the end of a CDATA section cannot contain spaces or newlines.

summary:

  • Features of the character area:
    • Displayed as it is (the written content will not be parsed by the xml parser)

6 DTD constraints (just understandable)

1 What is DTD

DTD (Document Type Definition), document type definition, used to constrain XML documents. Specifies the name of the element in the XML document, the name and order of the child elements, the attributes of the element, etc.

2 Implementation of DTD constraints and grammatical rules (understand DTD constraints, write xml files that conform to the specification)

During development, we will not write the DTD constraint document by ourselves. Usually, we write the corresponding XML document through the DTD constraint document provided by the framework.

Requirements: Next, we create a dtd constraint document, and then write a specification-compliant xml file according to the constraint document.

Let's create a new books.xml file first.

Step 1: Create a dtd folder under the project, then select the folder, right-click, and create a books.xml file

[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-oTtoRvkX-1687767096552)(imgs\8.bmp)]

Step 2: Let's write the content in the books.xml file first:

<?xml version="1.0" encoding="UTF-8"?>
<books>
	<book>
		<name>面试宝典</name>
		<author>锁哥</author>
		<price>78.8</price>
	</book>
	<book>
		<name>java从入门到精通</name>
		<author>黑旋风</author>
		<price>88.8</price>
	</book>
</books>

After the above four steps, we have finished writing the books.xml file, and then we start to write the DTD constraints.

We can understand the DTD constraints. As shown below, the above books.xml file introduces DTD constraints.

Simple DTD constraints are written, as follows:

We can directly copy the following constraints into the books.xml file we wrote above, and we can read it.

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	1.在xml内部定义dtd约束的声明 : <!DOCTYPE 根元素 [元素声明]>
	2.xml的元素(标签)的声明: <!ELEMENT 元素名称 (元素内容)>
		<!ELEMENT books (book)> 表示books标签下是book标签
	    <!ELEMENT book (name,author,price)> 表示的是book标签下出现的name,author,price子标签
	    <!ELEMENT name (#PCDATA)> 表示name标签中出现的内容是文本
 -->
 <!DOCTYPE books [
 	<!--约束根标签 book* 表示books标签下可以有多个book子标签
 		* + ? 和正则表达式中表示的意思是一样的
		* :  0 1 n
		+ : 1 n
		? :  0 1
 	-->
 	<!ELEMENT books (book*)>
 	<!--约束book标签-->
 	<!ELEMENT book (name,author,price)>
 	<!--约束name,author,price标签 
 		但是这三个标签下就是文本了
 		#PCDATA 表示标签下内容是文本
 	-->
 	<!ELEMENT name (#PCDATA)>
 	<!ELEMENT author (#PCDATA)>
 	<!ELEMENT price (#PCDATA)>
 ]>
<books>
	<book>
		<name>面试宝典</name>
		<author>锁哥</author>
		<price>78.8</price>
	</book>
	<book>
		<name>java从入门到精通</name>
		<author>黑旋风</author>
		<price>88.8</price>
	</book>
</books>

illustrate:

1) The tags that appear in xml are also called elements. Then the constraints we write can regulate which tags can appear in the xml. Nothing else can appear.

Therefore, the tags that appear in xml need to be declared by our developers in the dtd constraints. Only when this tag is declared can this tag appear in xml. If there is no statement in the constraint, it cannot appear in the xml.

Therefore, the tags that appear in xml need to use the following syntax (that is, the declaration syntax of elements in xml).

The declaration that defines the dtd constraints inside the xml:

 <!DOCTYPE 根元素 [元素声明]>

Declaration of elements (tags) of xml:

<!ELEMENT 元素名称 (元素内容)>

Element name: custom.

Element content includes: symbols, data types, and labels.

Common symbols: ? * + () |

Common data types: #PCDATA indicates that the content is text and cannot be a subtag.

Tags: are normal sub-tags.

2) Since the order of <!ELEMENT book (name,author,price)> is name,author,price, the following order must also be:

<book>
		<name>面试宝典</name>
		<author>锁哥</author>
		<price>78.8</price>
</book>

The sequence cannot be changed.

Next, add attributes to the tags of the above code, such as adding an address to the book attribute, indicating where to store the book.

The code after adding the attribute is as follows:

<!DOCTYPE books [
 	<!--约束根标签 book* 表示books标签下可以有多个book子标签
 		* + ? 和正则表达式中表示的意思是一样的
 	-->
 	<!ELEMENT books (book*)>
 	<!--约束book标签-->
 	<!ELEMENT book (name,author,price)>
 	<!--约束name,author,price标签 
 		但是这三个标签下就是文本了
 		#PCDATA 表示标签下内容是文本
 	-->
 	<!ELEMENT name (#PCDATA)>
 	<!ELEMENT author (#PCDATA)>
 	<!ELEMENT price (#PCDATA)>
 	<!ATTLIST book address CDATA "图书馆"
 					id      ID    #REQUIRED
 			>
 ]>
<books>
	<book address="藏经阁" id="a1">
		<name>面试宝典</name>
		<author>锁哥</author>
		<price>78.8</price>
	</book>
	<book id="a2">
		<name>java从入门到精通</name>
		<author>黑旋风</author>
		<price>88.8</price>
	</book>
</books>

Explanation of the above declaration attributes:

Element name: Indicates which tag the attribute is used on;

Attribute name: Indicates the attribute name added on the label;

Attribute Type: The attribute type to add.

The attribute types are as follows:

​ type description

​CDATA value is character data

​ (en1|en2|…) This value is a value in the enumerated list

​ID value is a unique id

Default value: Indicates the default value given to the attribute at the beginning.

​ Value Explanation

​ The default value of the value attribute

​#REQUIRED attribute value is required

​ #IMPLIED attribute is not required

​ #FIXED value attribute value is fixed



insert image description here

Guess you like

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