XML language what is XML?
XML refers to Extensible Markup Language (extensible Markup Language), which is a markup language , much like HTML. It is designed to transmit data rather than display it. XML tags are not predefined and require users to define their own tags. XML technology is published by the World Wide Web Consortium WWW Consortium, and is now followed by the XML1.0 specification published by the organization in 2000. XML is widely considered to be the most exciting new technology on the internet following java.
What problems does XML technology use to solve?
There are a lot of related data in real life, as shown in the figure on the right.
Question: How should such data be represented and handed over to the computer?
The fundamental purpose of the XML language is to describe the data that is related to that relationship.
XML is a common format for data interchange.
In the XML language, it allows the user to customize the label. A label is used to describe a piece of data, a label can be divided into start and end tags, between the start tag, and other tags can be used to describe other data to achieve the description of the data relationship.
The data in XML must be parsed by a software program to perform or display, such as IE, a parser called Parser(parser).
<?XML version= "1.0" encoding= "UTF-8"?><China> <Beijing> <Haidian></Haidian> <Fengtai></Fengtai> </Beijing> <Shandong> <Jinan></Jinan> <Qingdao></Qingdao> </Shandong> <Hubei> <Wuhan></Wuhan> <Jingzhou></Jingzhou> </Hubei></China>
Common XML Applications:
XML technology, in addition to preserving relational data, is often used as a software configuration file to describe the relationship between program modules. (Struts, spring, and Hibernate, as you will learn later, are all based on XML as configuration files)
In a software system, the flexibility of the system can be improved through XML configuration files. That is, the behavior of the program is configured through an XML file, not hard-coded.
Data exchange: Used to exchange data between different languages
Small database: Used to store data when the database is stored.
XML syntax: XML syntax:
An XML file is divided into the following sections:
Document Declaration element Property Comment CDATA Area, special character processing instruction (pi:processing instruction)
Document Declaration
When you write an XML document, you need to declare the XML document with the document declaration first. and must appear in the first line of the document. And you must specify
The simplest syntax:
<? XML version= "1.0" ?>
Use the encoding property to describe the character encoding used by the document. The file encoding to be saved on disk is consistent with the encoding of the Declaration. Such as:
<? XML version= "1.0" encoding= "GB2312" ?>
Use the standalone property to describe whether the document is independent or dependent on other documents. Such as:
<? XML version= "1.0" standalone= "yes" ?>
Yes without introducing external files, no need to be introduced. (not commonly used)
Element tags
An XML element refers to a label that appears in an XML file.
A label is divided into start and end tags (cannot be omitted). A label is written in the following ways:
Include tag body: <mytag>some content</mytag>
Does not include the label body:<mytag/>
Several sub-labels can be nested within a tag, but all tags must be properly nested, and no cross-nesting is allowed.
<mytag1><mytag2></mytag1></mytag2> wrong!
An XML document must have only one root tag, and the other tag is a child tag or a grandchild tag for that root tag.
Label spaces, line breaks
For all whitespace and line breaks that appear in the XML tag, the XML parser is treated as a label content. For example, the meaning of the following two paragraphs is different.
Since spaces and line breaks are processed as raw content in XML, the "good" writing habits of using line wrapping and indentation to make the contents of the original file readable in an XML file can be forced to change.
Naming conventions
An XML element can contain letters, numbers, and other visible characters, but must adhere to some of the following specifications:
Case sensitivity, such as,<p> and <p>, are two different tokens.
You cannot start with a number or a "-" (middle dash).
You cannot start with XML (or XML, or XML, and so on).
Cannot contain spaces.
The middle of the name cannot contain a colon (:).
Property
An element can have multiple properties, each with its own name and value, for example: <mytag name= "Value" .../>
Attribute values must be enclosed in quotation marks (single or double quotation marks).
The naming convention for property names is the same as for element naming conventions
The attributes in the element are not allowed to be duplicated
In XML technology, the information represented by the tag attribute can also be changed to be described in the form of a child element, for example:
< MyTag > < name > < FirstName /> < LastName /> </ name > </ MyTag >
Comments
The comment syntax in XML is:<!--This is a comment--
Attention:
Cannot have comments before XML declaration
Annotations cannot be nested, for example:
Escape character
Note:< and & symbols to use escape characters, > "' Can not use escape characters.
CDATA Zone
CDATA is the abbreviation for character data
Role: The label as ordinary text content;
Syntax: <! [Cdata[content]]>
<! [cdata[ <itcast>www.itcast.cn</itcast>]]>
The red part above is treated as plain text rather than as a label
Processing instructions
Processing instructions, referred to as pi (processing instruction).
Role: Used to direct how the software parses an XML document.
Syntax: Must be "<?" As the beginning, with "?>" as the end. Common Processing Instructions:
Common Processing Instructions:
XML declaration:
<?xml version= "1.0" encoding= "GB2312"?>
Xml-stylesheet directive: function: Indicates the CSS style XSL used by the XML document.
<?xml-stylesheet type= "Text/css" href= "Some.css"?>
Note: Label elements that are named in Chinese do not work
Summary of XML syntax rules
All XML elements must have a close tag
XML tag-to-case-sensitive XML
must be correctly nested in order
The XML document must have a root element (only one)
Attribute values for XML must be quoted
Special characters must be escaped---CDATA
Whitespace in XML, carriage return is persisted when parsing
XML constraints: Why constraints are required
1.XML are user-defined labels, if there is a small error, the software program will not be able to correctly obtain the contents of the file and error. (eg: Tomcat)
In 2.XML technology, a document can be written to constrain an XML writing specification, which is called a constraint.
Two concepts:
Well-formed XML: XML that follows XML syntax
Valid xml: Follows the XML of the constrained document
3. In summary: The constraint document defines the allowed element names, attributes, and the order in which elements appear in the XML, among others.
XML Constraints Overview
1. What is an XML constraint
In XML technology, you can write a document to constrain an XML
The writing specification of a document, which is called an XML constraint.
2. Why XML constraints are required
3. Common constraint Techniques
XML DTD
XML Schema
DTD constrained Fast bundle starter DTD overview
1.DTD (document type definition), all referred to as the documentation type definition.
2. After the completion of the DTD, and the constraints of success, can summarize the process of writing, more convenient memory.
Complex Tags: <! ELEMENT tag name (child node) >
Simple tag: <! ELEMENT tag name (#PCDATA) >
Introduction of dtd:<! DOCTYPE root node SYSTEM "DTD Address" >
Associating a DTD with an XML document three ways
DTD constraints can be written as a single file or within an XML file
1. Using an internal DTD
<! DOCTYPE root node [DTD code]>
2. Using an external DTD
<! DOCTYPE root node SYSTEM "DTD Address" >
3. Using a network DTD
<! DOCTYPE root node Public "DTD name", "DTD address" >
Common use of network DTDs to constrain a Struts2 framework
To write a DTD within an XML file
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?><!DOCTYPE Bookcase [<! ELEMENT Bookshelf (book +)> <!ELEMENT Book (title, author, Price)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT Price (#PCDATA)>]><Bookshelf> <Book> <title>Java Employment Training Tutorials</title> <author>Zhang Xiaoxiang</author> <Price>39.00 USD</Price> </Book> ...</Bookshelf>
Introducing an external DTD document
XML uses the DOCTYPE declaration statement to indicate the DTD document it follows, in two forms:
When referencing a DTD document locally, use the following method:
<! DOCTYPE root element SYSTEM "DTD document Path" >
Such as:
<! DOCTYPE Bookshelf SYSTEM "BOOK.DTD" >
When referencing a DTD document on a public network, use the following method:
<! DOCTYPE root element Public "DTD name" "DTD document URL" >
Such as:
<! DOCTYPE Web-app Public "-//sun Microsystems, INC.//DTD Web application 2.3//en" "http://java.sun.com/dtd/ Web-app_2_3.dtd ">
DTD constraint Syntax details
Element definition attribute definition entity definition
elements (Element) definition
Use the element keyword in a DTD document to declare an XML element.
Syntax: <! Element name Usage Rules >
Usage Rules:
(#PCDATA): Indicates that the body content of an element can only be normal text. (Parsed Character Data)
Empty: Used to indicate that the body of the element is null. such as <br/>
Any: Used to indicate that the body content of an element is of any type.
(child Element): Indicates the child element contained in the element
Define child elements and describe their relationships:
If the child elements are separated by commas, the XML document must be written in the order in which they are declared.
such as: <! ELEMENT FILE (Title,author,email)
If the child element uses the "|" Separate, explain any choice.
such as: <! ELEMENT FILE (title| author| EMAIL)
With +, *,? To indicate the number of occurrences of an element
If there is no +* behind the element?: Indicates that only one occurrence must occur
+: Indicates at least one occurrence, one or more times
*: Denotes dispensable, 0 times, one or more times
?: Indicates that there can or may not, and some words can only be once. 0 times or once such as: <! ELEMENT MYFILE (title*, AUTHOR?, EMAIL) * | COMMENT) >
Javaweb's XML explanation