web development (a) xml Introduction and grammar

1.1 xml Overview

What is XML

  • XML: Extensible Markup Language ( EXtensible Markup Language )

    It is an XML markup language , very similar to HTML, tags are customizable.

    Scalable: Custom content, like how to write on how to write

    Mark, is the label.

    Such as: <user>标签体</user> 或 <student></student><person> </ person>

  • W3C (World Wide Web Consortium) released in February 1988 Version 1.0, released in February 2004 and version 1.1, not version 1.1 as a single backward compatible with version 1.0, so no one with 1.1. Meanwhile, in February 2004, W3C has released a 1.0 version of the third edition. We want to learn or 1.0 version.

The main difference between XML and HTML,

  • xml tags are customizable, html tags are predefined.
  • xml strict syntax, html syntax loose.
  • xml is stored data, html is showing data.

The role of xml

  • Data storage
<?xml version="1.0" encoding="UTF-8"?>
<persons>
	<person id="p001">
		<name>张三</name>
	</person>
	<person id="p002">
		<name>李四</name>
	</person>
</persons>

Similar to java code

class Person{
	String id;
	String name;
}

public void test(){
	HashSet<Person> persons = new HashSet<Person>();
	persons.add( new Person("p001","张三") );
	persons.add( new Person("p002","李四") );
}
  • Profile -> Data storage
<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<bean className="com.ccc_00_Bean.User">
		<property name="username" value="jack"></property>
	</bean>
</beans>

Similar to java code

class Bean{
	private String username;
	private String pws;
	//补全set\get方法
}
import com.ccc_00_Bean.User;
public static void main(){
    Class clzzz = Class.forName("com.ccc_00_Bean.User");
    Object obj = clazz.newInstance();
    Method method = clazz.getMethod("setUsername",String.class);
    method.invoke(obj,"jack");
}

1.2 xml constituent elements of the (grammatical)

XML file

xml文件扩展名必须为xml
xml内容必须满足以下要求

	1、固定的文档声明

	2、合格的元素和属性

	3、正确的注释

	4、符合要求的特殊字符编写规则

Document declaration

  • XML document declaration format:
<?xml version="1.0" encoding="UTF-8"?>
  1. Documentation must declare an end to begin with <xml, to??>;
  2. Document declaration must start from the document line 0 0 position;
  3. Document declaration only two attributes:
1. versioin:指定XML文档版本。必须属性,因为我们不会选择1.1,只会选择1.0;
2. encoding:指定当前文档的编码。可选属性,默认值是utf-8;

Elements (tags)

  • Element element
<bean></bean>
  1. XML document element is the most important part,
  2. Structure began to label the common elements, elements of the body, an end tag.
例如: <hello>大家好</hello>
  1. Body element: body element can be an element, it can be text
例如:<b><a>你好</a></b>
  1. Empty elements: empty element tag is only the beginning, but no end tag, but the elements must own closed
例如:<c/><img />
  1. Naming elements:
	a. 区分大小写
	b. 不能使用空格,不能使用冒号:
	c. 不建议以XML、xml、Xml开头

note:

格式化良好的XML文档,必须只有一个根元素。

Attributes

  • Property attribute
<bean  className=”zhangsan”  id=’aa011’></bean>
  1. Attribute part of the element must appear at the beginning of the tag element, the attributes and elements separated by spaces, a space is divided among a plurality of attributes
  2. Definition Format attribute: attribute name = attribute value, wherein the attribute value must be a single or double quotes primer
  3. A 0 ~ N element can have attributes, but can not appear in an element of the same name attribute
  4. Attribute names can not use spaces, colon and other special characters, and must start with a letter
  5. No order between the plurality of attributes

Note

XML的注释,以“<!--”开始,以“-->”结束。注释内容会被XML解析器忽略!

Escape character

Because many symbols have been used in the XML document structure, like the element or attribute value using these symbols must use an escape character, for example: "<", ">", "", "", "", "& . " 3
<5 && 6> 7
Here Insert Picture Description
example:

1<3 == 5  在xml中的写法  1&lt;3==5

1.3 xml constraints

In XML technology, the constraint can write a document to write an XML document specification, which is called XML constraints.
Common xml constraints: DTD, Schema
Here Insert Picture Description
purposes:

Constraints do not need to write a document, you can almost understand it

By constraining the document you can write an xml document (format having a fixed format, predetermined).

Others bound documents are written, we take over with

1.3.1 DTD constraints

What is a DTD

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

DTD key requirements

Development, we rarely write your own DTD bound documents, usually we are all bound by the framework provided by the DTD document, written in the corresponding XML documents. Common framework using DTD constraints have: struts2, hibernate and so on.

DTD provided by " bean.dtd write XML"

<?xml version="1.0" encoding="UTF-8"?>
<!--
	模拟spring规范,如果开发人员需要在xml使用当前DTD约束,必须包括DOCTYPE。
	格式如下:
	<!DOCTYPE beans SYSTEM "bean.dtd">
-->
<!ELEMENT beans (bean*,import*) >
<!ELEMENT bean (property*)>
<!ELEMENT property (#PCDATA)>

<!ELEMENT import (#PCDATA)>

<!ATTLIST bean id CDATA #REQUIRED
			   className CDATA #REQUIRED
>

<!ATTLIST property name CDATA #REQUIRED
				   value CDATA #REQUIRED
>

<!ATTLIST import resource CDATA #REQUIRED>

Case realization

Finish writing xml content

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans SYSTEM "bean.dtd">
<beans>
	<bean id="" className=""></bean>
	
	<bean id="" className="">
		<property name="" value=""></property>
		<property name="" value=""></property>
	</bean>
	
	<import resource=""></import>
	<import resource=""></import>

</beans>

How to use the DTD constraint document to write xml document

Steps for usage:
	1.把DTD约束文档中以<!DOCTYPE开头的代码复制到xml文档中
		<!DOCTYPE beans SYSTEM "bean.dtd">
		!DOCTYPE:dtd约束文档的固定语法声明
		beans:要使用dtd约束文档,根元素必须叫beans
		SYSTEM:系统,说明dtd约束文档来源于本地的操作系统
		"bean.dtd":dtd约束文档的路径(相对路径)一般我们都会把dtd约束文档和xml文档放在同一个文件夹
   2.根据dtd文档根元素的要求,写出根元素beans
   3.根据提示,写出其他的元素和属性(< alt+/)

1.3.2 Schema Constraint

What is Schema

Schema is a new XML document constraints;
Schema many powerful than the DTD, DTD is a replacement;
Schema is itself an XML document, but the extension Schema document named xsd, rather than the xml.
Schema more powerful, more sophisticated data types
Schema support namespace (namespace)

Here Insert Picture Description

Schema key requirements

With a DTD, you can write xml documents required by the document schema constraints. Common framework uses schema are: Spring, etc.

By providing a " bean-schema.xsd write xml document."

<?xml version="1.0" encoding="UTF-8"?>
<!-- 
	模拟spring规范,如果开发人员需要在xml使用当前Schema约束,必须包括指定命名空间。
	格式如下:
	<beans xmlns="http://www.itcast.cn/bean"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.itcast.cn/bean bean-schema.xsd"
	>
1. <element>声明元素(标签)
2.每个元素都必须确定类型
	complexType:复杂类型
	simple Type:简单类型,一般不用,大部分都是复杂类型
3.需要明确子标签的出场顺序
	<choice>选择 a|b
	<sequence>顺序 a,b
	<all>任意
		minOccurs:最少出现次数
		maxOccurs:最大出现次数,unbounded:不限制(没有边)
4.<attribute>用于给元素声明属性
	use:设置属性使用 optional:可选 required:必须

-->
<schema xmlns="http://www.w3.org/2001/XMLSchema"
		targetNamespace="http://www.itcast.cn/bean"
		xmlns:xsd="http://www.w3.org/2001/XMLSchema"
		xmlns:tns="http://www.itcast.cn/bean"
		elementFormDefault="qualified">
	<!-- 声明根标签 -->
	<element name="beans">
		<complexType>
			<choice minOccurs="0" maxOccurs="unbounded">
				<element name="bean">
					<complexType>
						<sequence minOccurs="0" maxOccurs="unbounded">
							<element name="property">
								<complexType>
									<attribute name="name" use="required"></attribute>
									<attribute name="value" use="required"></attribute>
								</complexType>
							</element>
						</sequence>
						<attribute name="id" use="required"></attribute>
						<attribute name="className" use="required"></attribute>
					</complexType>
				</element>
				<element name="import">
					<complexType>
						<attribute name="resource" use="required"></attribute>
					</complexType>
				</element>
			</choice>
		</complexType>
	</element>
</schema>
  • l Case document with a "namespace" are using the "default namespace" and "Display name space" were introduced, so the documentation and xsd: schema role of the same.
    Here Insert Picture Description

Case realization

Finish writing xml content

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.itcast.cn/bean"
	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	   xsi:schemaLocation="http://www.itcast.cn/bean bean-schema.xsd"
>
	<bean id="" className=""></bean>
	<bean id="" className="">
		<property name="" value=""/>
		<property name="" value=""/>
	</bean>
	
	<import resource=""/>
	<import resource=""/>
</beans>
Schema约束文档:
	1.每个Schema约束文档,都必须有一个名字,这个名字就是命名空间
		要求:全球唯一,一般使用公司的域名+项目名称+...
		targetNamespace="http://www.itcast.cn/bean/demo01/...."

	2.在xml文档中想要使用Scheme约束文档,必须声明使用的是哪个Schema约束文档
		a.默认声明:只能有一个
				xmlns="default namespace"
				xmlns="http://www.itcast.cn/bean"
		b.显示声明:可以有多个
				xmlns:别名1=”http://www.itcast.cn/bean”
				xmlns:别名2="http://www.itcast.cn/bean"
	3.想要使用Schema约束文档,必须确定约束文档的位置
		a.先确定官方文档的位置
				xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		b.通过官方文档确定我们自己的Schema约束文档的位置
				xsi:schemaLocation="{namespace} {location}"
									 命名空间	 具体位置
	4.使用
		使用默认声明,直接写元素名称<bean></bean>
		使用显示声明:格式
			别名:元素名称
			<别名1:bean></别名1:bean>

How to use the schema bound documents, write xml document

Root element tag 1. Each constraint schema document has a beginning, xml document copy to add a root end tag
2. prompt, the other write elements and attributes (<alt + /)

Namespace (grammar)

What is the name space

Name conflict occurs when a file if you use multiple XML Schema documents, and these documents Schema defines the elements of the same name. It's like a Java file using the import java.util. * And import java.sql. * When in use the Date class, then there is a clear Date Date under which the package.

In short namespace name is used to process elements and attributes conflict with the Java package is the same purpose. If each element and attribute has its own name space, then it will not name conflict occurs, like a package where each class has its own the same, then the class name will not conflict.

Xml document constraints and relationships

When asked W3C Schema constraint specification, it provides "official document constraints." We official documents must be "custom schema bound document," development "custom document" framework provided by the authors. We provide "custom document" limited write their own xml document.
Here Insert Picture Description

Namespace declaration
默认命名空间:<xxx xmlns=””>,使用<标签>
显式命名空间:<xxx xmlns:别名=””>,使用<别名:标签>

Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_45083975/article/details/92002954