A good memory is better than a bad pen. 61-xml Document structure and property descriptions

Source: Internet
Author: User
Tags cdata xml parser

XML we use a lot of simple XML document structure We also know that attributes, it seems to be so-so-so know, but in the further understanding of XML, it is not enough to find out what they know, to organize the information.

1. XML declaration

An XML document consists of a set of entities that are identified by using a unique name. Always start with a declaration that specifies that the document follows the XML1.0 specification. XML also has a logical structure in which the composition of a document consists of declarations, elements, annotations, character references, and processing instructions logically.
Here is the code snippet:

2. Root element

Each XML file must have and have only one root element. Used to describe document functionality. You can customize the root element. Root is the root element in the following example. Here is the code snippet:

<root>...................</root>
3. XML code

Create custom elements and attributes based on your application needs. The label includes the angle brackets and the text in the angle brackets. An element is the basic unit of XML content. Elements include the start tag, the end tag, and the content between the tags.
Here is the code snippet:

<title>XML是可扩展标记语言</title>

The entire line is collectively referred to as an element, which is the label, XML is Extensible Markup Language is character data

XML的标记有:标签,注释,处理指令,DTD和引用等。注释在XML中,注释与HTML的一样,都是用<!-- 注释 -->指定注释。要遵循以下规则:在注释中不应包含有“-”“—”等字符,以免XML解释器产生混淆。注释不能放在标签中注释不能写在第一行。第一行是XML声明。注释不能嵌套可跨行注释,与java中的注释块功能一样。处理指令凡是以<?开始, ?>结束的都是处理指令。XML声明就是一个处理指令。标签间的字符数据的分类标签间的字符数据可以是除了“<”以外的任何合法(Unicode)字符。“<”字符是预留作为标签的开始字符。字符数据分以下两类:PCDATA(是指将要通过解析器进行解析的文本)CDATA (是指不要通过解析器进行解析的文本)其中不允许中CDATA块之内使用字符串“]]>”,因为它表示CDATA块的结束。

Entity

实体可以是常用的短语,键盘字符,文件,数据库记录或任何包含数据的项。在XML中,有时实体内包含了些字符,如&,<,>,",‘等。这些均需要对其进行转义,否则会对XML解释器生成错误。字符转义后字符<<>>""‘&apos;&&

Any of the above characters in the XML will need to be escaped to pass.

4. XML entity
<! Entity entity reference name "reference content" >Entities are divided into two categories: 1, General entities (format:& entity reference name;) 2, parameter entity (format:% entity reference name;) General entities, entities that can appear anywhere in an XML document are called generic entities. An entity can be declared as an internal or external entity. External entities are divided into sysytem and public two kinds. Sysytem refers to the local computer, public refers to the common computer, and the external entity format is as follows:<! ENTITY Reference name SYSTEM(public) "URI Address" >The rules for managing entity references include: Before referencing an entity, you must first declare the entity in the XML document. Entity references should not contain any empty cells. For example:& title; &title; it's all wrong. The XML file referenced by the entity must be well-formed. Entity references can override regular character data, or you can use entity references in label properties. Such as<title value="&titlue" />DOCTYPE declaration in the XML document,<! Doctype[...] >The declaration follows the XML declaration. The entity must also be declared in the DOCTYPE declaration. The syntax is as follows: The following is the code snippet:<?xml version= "1.0" unicode= "UTF-8" ><! Doctype[..... In this declaration entity <! Entity entity reference name reference content >]> code example: Here is the code snippet: <?xml version= "1.0" encoding= "GBK"?><! DOCTYPE root[<! ENTITY Titlue "reference character 1" ><! ENTITY titlue2 "reference character 2" >]><root><title value="&titlue;" >&titlue;</title><title2><value><a>&titlue2;</a></value></title2></root>In IE, the Open XML file is displayed as: The following is the code snippet:<?xml version= "1.0" encoding= "GBK"?><! DOCTYPE Root (View Source for full DOCTYPE ...) >-<root><title value="reference character 1">Reference character 1</title>-<title2>-<value><a>Reference character 2</a></value></title2></root>

is a well-formed XML document when the following conditions are true:
At least one element is required
XML tags are case-sensitive
The end tag should be used correctly
The right nesting
Legal tags should be used: labels must start with a letter, underscore, or colon, followed by a combination of letters, numbers, periods, colons, underscores, and hyphens. But there can be no spaces. It should also not start with XML, because XML is a reserved word.
Tag name length
Valid attributes should be defined
The document should be validated
In XML, all values are taken as strings.

5. Parser
There are two types of parsers: 1, non-validating parsers: It checks if the document is well-formed 2, validates the parser: check the validity of the document using a DTD (with Apache Xerces and Oracle XML Parser) DTD: (document) The general structure of the document type structure DTD is as follows:<! DOCTYPE dtd-name[<! ELEMENT element-name (element-content type) ><! Attlist element-name attribute-name attribute-type default-value>]>Element-name: element name Element-content type: element type Attribute-name: Property name Attribute-type: attribute type default-value: default null element: Empty, Specifies that the element has no child elements or character data.<! ELEMENT element-name (EMPTY) >Elements with Data:<! ELEMENT element-name (#CDATA) >Contains data that is not parsed by the parser<! ELEMENT element-name (#PCDATA) >Contains data to be parsed by the parser<! ELEMENT element-name (any) >A child element that contains 0 or more than 0 any claim types, and the element with the child elements of the character data<! ELEMENT element-name (child-element-name1,child-element-name2 ...) >When multiple child elements are separated by a number, the same element appears only once<! ELEMENT element-name (child-element-name) >declares that the same element appears only 0 or one time<! ELEMENT element-name (child-element-name?) >declares that the same element appears only one or more times<! ELEMENT element-name (child-element-name+) >declares that the same element appears only 0 or more times<! ELEMENT element-name (child-element-name*) >Property declaration: Default property Value: Defaults property value<! Attlist element-name attribute-name CDATA "default-value" >Implied property value: Optional<! Attlist element-name attribute-name #implied>Required property Value: Specifies that the property is required<! Attlist element-name attribute-name #REQUIRED>Fixed property value: The property value is pinned so that the author cannot change<! Attlist element-name attribute-name CDATA(PCDATA) #FIXED " value">Enumerated property Type: A member that makes an attribute value a fixed set of legal values<! Attlist element-name attribute-name (1 | 2 | ....) "default-value" >DTD example: Internal DTD The following is the code snippet:<?xml version= "1.0" encoding= "GBK"?><! DOCTYPE root[<! ELEMENT root (book*) ><! ELEMENT Book (name,title,versions,address) ><! ELEMENT name (#PCDATA) ><! ELEMENT title (#PCDATA) ><! ELEMENT versions (#PCDATA) ><! ELEMENT address (#PCDATA) ><! Attlist Title value CDATA #REQUIRED ><! Attlist Address value CDATA #FIXED "Guangdong, China" ><! Attlist versions Value (1.0 | 2.0) "1.0" ><! ENTITY addr "Zhuhai, Guangdong, China" >]><root><book ><name>Xml</name><title value="a">XML Extensible language</title><versions value="1.0">1.0</versions><address value="Guangdong, China">&addr;</address></book ><book ><name>Xml</name><title value="">XML Extensible language</title><versions>1.0</versions><address value="Guangdong, China">&addr;</address></book ></root>The external DTD uses the following syntax:<?xml version= "1.0" encoding= "GBK"?><! DOCTYPE root element name SYSTEM "External DTD file uri" ><root>........<root>The content of the external DTD file here is: The following is the code snippet:<?xml version= "1.0" encoding= "GBK"?><! ELEMENT Root (book *) ><! ELEMENT Book (name,title,versions,address)  ><! ELEMENT name (#PCDATA) ><! ELEMENT title (#PCDATA) ><! ELEMENT versions (#PCDATA) ><! ELEMENT Address (#PCDATA) ><! Attlist title value CDATA #REQUIRED><! Attlist Address value CDATA #FIXED "Guangdong, China" ><! Attlist versions value (1.0 | 2.0 ) "1.0" ><! ENTITY addr "Zhuhai, Guangdong, China" >

A good memory is better than a bad pen. 61-xml Document structure and property descriptions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.