Introduction to XML and Basic Syntax

1. Introduction to xml (understanding)

* Extensible Markup Language: Extensible Markup Language
** Markup Language: HTML is a markup language
- it also uses tags to operate
** Extensible:
- The tags in html are fixed, and each tag has a specific meaning <h1 > <br/> <hr/>
- xml tags can be defined by themselves, and Chinese tags can be written <person></person>, <cat></cat> * xml usage ** html is used to display data, xml is also Can display data (not main function) ** xml main function, in order to store data * xml is a technology released by w3c organization * xml has two versions 1.0 1.1








- Use version 1.0, (version 1.1 is not backward compatible)

Second, the application of xml

* Data transmission between different systems
** Data transmission between qq
         is beneficial to the later maintenance of the program

* Used to represent data related to life * Often used in file configuration * For example, when connecting to the database now, you must know the user name of the database and password, database name 



* If you modify the information of the database, you do not need to modify the source code, just modify the configuration file.

Three, xml syntax

        1. Documentation Statement

                * Create a file with a suffix of .xml
* If you write xml, the first step must have a document declaration (after writing the document declaration, it means to write the content of the xml file)
** <?xml version="1.0" encoding=" gbk"?>
*** Document declaration must be written in the first line and first column

* Attributes
- version: xml version 1.0 (use) 1.1
- encoding: xml encoding gbk utf-8 iso8859-1 (excluding Chinese)
- standalone : Do you need to rely on other files yes/no * xml Chinese garbled problem ** The encoding when saving is the same as the encoding when setting and opening, there will be no garbled characters



        2. Element (tag) definition

        ** A tag definition must have a beginning and an end: <person></person>
        ** A tag has no content and can end within a tag: <aa/>
        ** Tags can be nested, but must be nested reasonably
*** Reasonable Nested <aa><bb></bb></aa>
*** Unreasonable nested <aa><bb></aa></bb>: this way is incorrect
        ** in an xml , there can only be one root tag, and other tags are tags below this tag

        ** In xml, both spaces and newlines are parsed as content
        **** The meanings of the following two pieces of code are different
                * <aa>1111111 </aa>
                * <aa>
                    11111111111
                 </aa> ** xml tags can be Chinese

        

         ** Naming rules for tags in xml
         (1) xml code is case-sensitive
         <p> <P>: these two tags are different
         (2) xml tags cannot start with numbers and underscores (_)
         <2a> < _aa>: this is incorrect
         (3) xml tags cannot start with xml, XML, Xml, etc.
         <xmla> <XmlB> <XMLC>: these are incorrect
         (4) xml tags cannot contain spaces and colons
         <ab> <b:c> : these are incorrect

        3. Attribute Definition

        * html is a markup document and can have attributes
        * xml is also a markup document and can have attributes

        * <person id1="aaa" id2="bbb"></person>

        ** Requirements for attribute definition
        (1) On a tag There can be multiple attributes
        <person id1="aaa" id2="bbb"></person>
        (2) attribute names cannot be the same
        <person id1="aaa" id1="bbb"></person>: this is not Correct, there cannot be two id1s

        (3) Use = between the attribute name and the attribute value, and use quotation marks for the attribute value (either single or double quotation marks)

                        (4) The name specification of the xml attribute is consistent with the name specification of the element

        4. Notes

        * Format:

                                <!-- xml comment --> 

         **Notes
         **** Comments cannot be nested
         <!-- <!-- --> -->
         <!-- <!-- <sex>nv</sex>--> -->

        ** Comments cannot be placed on the first line, the first line and first column must be placed in the document declaration

       5. Special characters

        * If you want to display a<b in xml, it cannot be displayed normally, because < is used as a tag
        * If you want to display it, you need to escape the special character <
        ** < <

           >    >

        6.CDATA area (understand)

        * It can solve the operation that multiple characters need to be escaped if(a<b && b<c && d>f) {}
        * Put these contents in the CDATA area, no need to escape
        ** Writing
        <![CDATA [content]]>
        - code
        <![CDATA[ <b>if(a<b && b<c && d>f) {}</b> ]]>

        ** Treat special characters as text content, not labels

        7. PI instruction (processing instruction) (understand)

        * You can set styles in xml
        * Writing: <?xml-stylesheet type="text/css" href="path to css"?>

        * Set the style, only works for English label names, not for Chinese label names.

<?xml version="1.0" encoding="GBK"?>
<?xml-stylesheet type="text/css" href="a.css"?>
<person>
	<name>张三</name>
	<age>22</age>
	<aaa>1<2</aaa>
	<![CDATA[<p>www.badu.com</p>]]>
</person>
        ** Summary of xml syntax
All XML elements must have closing tags
XML tags are case sensitive
XML must be nested in the correct order
XML documents must have a root element (only one)
XML attribute values ​​must be quoted
Special characters must be escaped --- CDATA

Whitespace, carriage return and line feed in XML are preserved during parsing

Four, xml constraints

        * Why do you need constraints?
        ** For example, now define a person's xml file, and only want to save the person's information in this file, such as name age, etc., but if you
            write a tag <cat> in the xml file, it can be displayed normally because it conforms to the grammar specification . But cats are definitely not human information. XML tags are customized. Technology is required to
            specify elements that can only appear in XML. At this time, constraints are required.

        * The technology of xml constraints: dtd constraints and schema constraints (understand)

Five, the quick start of dtd

* Create a file suffix .dtd

steps:
(1) See how many elements there are in the xml, and how many elements there are, write a few <!ELEMENT> in the dtd file
(2) Determine whether the element is a simple element or a complex element
- complex Element: element with child elements
<!ELEMENT element name (child element)>
- simple element: no child element
<!ELEMENT element name (#PCDATA)>
(3) need to introduce dtd file
<!DOCTYPE root element in xml file Name SYSTEM "path to dtd file"> ** Open the xml file and open it with a browser. The browser is only responsible for verifying the syntax of the xml, not the constraints . ** If you want to verify the constraints of the xml, you need to use a tool ( myeclipse tool) ** Open the myeclipse development tool *** Create a project *** Create an xml file and a dtd file under the src directory of the project *** When the dtd file is introduced in the xml, the xml outside the constraints is written label, it will prompt an error









xml document demo:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE person SYSTEM "1.dtd">
<person>
	<student>
		<name>张三</name>
		<age>18</age>
	</student>
	<student>
		<name>李四</name>
		<age>20</age>
	</student>
</person>

The xml's dtd constraints document:

<!ELEMENT person (student+)>
<!ELEMENT student (name,age)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>

Six, three introduction methods of dtd

         1. Introduce external dtd file
<!DOCTYPE root element name SYSTEM "dtd path"> 2. Use internal dtd file <!DOCTYPE root element name [ <!ELEMENT person (name,age)> <!ELEMENT name (#PCDATA )> <!ELEMENT age (#PCDATA)> ]> 3. Use an external dtd file (dtd file on the network) <!DOCTYPE root element PUBLIC "DTD name" "DTD document URL"> - Learn the framework struts2 later Use configuration file to use external dtd file - <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"       "http://struts.apache.org/dtds/struts-2.0.dtd" >











Seven, use dtd to define elements

* Syntax: <!ELEMENT element name constraint>

* Simple element: element with no child elements
<!ELEMENT name (#PCDATA)>
*** (#PCDATA): Constraint name is string type
*** EMPTY : Element is empty (no content)
- <sex></sex>
*** ANY:any
* complex element:
<!ELEMENT person (name,age,sex,school)>
- child element can only appear once
* <!ELEMENT element name ( sub-element)> * indicates the number of

occurrences of the sub-element +
: indicates one or more times
? : indicates zero or one time
* : indicates zero or more times * Sub-elements are directly separated by | ** means that the element can only appear in any one of them






<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE books[
	<!ELEMENT books (book+)>
	<!ELEMENT book (name,writer,time,price)>
	<!ELEMENT name (#PCDATA)>
	<!ELEMENT writer (#PCDATA)>
	<!ELEMENT time (#PCDATA)>
	<!ELEMENT price EMPTY>
]>
<books>
	<book>
		<name>三体</name>
		<writer>刘慈欣</writer>
		<time>2008.1</time>
		<price></price>
	</book>
	<book>
		<name>Ordinary World</name>
		<writer>路遥</writer>
		<time>1986.12</time>
		<price></price>
	</book>
</books>

Eight, use dtd to define attributes

* Syntax     <!ATTLIST element name
attribute name attribute name attribute type attribute constraints
    > * attribute type - CDATA: string - <!ATTLIST birthday ID1 CDATA #REQUIRED > - enumeration: Indicates that values ​​can only appear in a certain range, but only One of the **traffic light effects ** can appear at a time (aa|bb|cc) - <!ATTLIST age ID2 (AA|BB|CC) #REQUIRED > - ID: The value can only start with a letter or an underscore - <! ATTLIST name  ID3 ID #REQUIRED > * Constraints of the attribute - #REQUIRED: The attribute must exist - #IMPLIED: The attribute is optional - #FIXED: Represents a fixed value #FIXED "AAA" - The value of the attribute must be the fixed value set value - <!ATTLIST sex ID4 CDATA #FIXED "ABC" > - direct value * do not write attribute, use direct value * write attribute, use set that value - <!ATTLIST school

































ID5 CDATA "WWW"

>

9. Definition of Entity

* Syntax  <!ENTITY entity name "entity value">

                    Using Entity: &entity name; 

*** <!ENTITY TEST "Xinhua">
*** Quote: &TEST; *Note * The definition entity needs to be written in the internal dtd,



 If it is written in an external dtd, under some browsers, the content cannot be obtained

w3cschool example code demonstration: (you can understand it)

<!ELEMENT TVSCHEDULE (CHANNEL+)>
<!ELEMENT CHANNEL (BANNER,DAY+)>
<!ELEMENT BANNER (#PCDATA)>
<!ELEMENT DAY (DATE,(HOLIDAY|PROGRAMSLOT+)+)>
<!ELEMENT HOLIDAY (#PCDATA)>
<!ELEMENT DATE (#PCDATA)>
<!ELEMENT PROGRAMSLOT (TIME,TITLE,DESCRIPTION?)>
<!ELEMENT TIME (#PCDATA)>
<!ELEMENT TITLE (#PCDATA)>
<!ELEMENT DESCRIPTION (#PCDATA)>

<!ATTLIST TVSCHEDULE NAME CDATA #REQUIRED>
<!ATTLIST CHANNEL CHAN CDATA #REQUIRED>
<!ATTLIST PROGRAMSLOT VTR CDATA #IMPLIED>
<!ATTLIST TITLE RATING CDATA #IMPLIED>
<!ATTLIST TITLE LANGUAGE CDATA #IMPLIED>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE TVSCHEDULE SYSTEM "cctv.dtd">
<TVSCHEDULE NAME="CCTV">
	<CHANNEL CHAN="5">
		<BANNER></BANNER>
		<DAY>
			<DATE></DATE>
			<HOLIDAY></HOLIDAY>
		</DAY>
	</CHANNEL>
	<CHANNEL CHAN="6">
		<BANNER></BANNER>
		<DAY>
			<DATE></DATE>
			<PROGRAMSLOT VTR="xxx">
				<TIME></TIME>
				<TITLE></TITLE>
			</PROGRAMSLOT>
		</DAY>
	</CHANNEL>
</TVSCHEDULE>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324679428&siteId=291194637