XML and Tomcat study notes

#Xml & Tomcat

##Xml

eXtendsible markup language Extensible markup language

###What's the use of XML?

  1. Can be used to save data

  2. Can be used as configuration file

  3. Data transmission carrier

icon

##Define xml

It's actually a file, the file suffix is ​​.xml

###. Document Statement

	简单声明, version : 解析这个xml的时候,使用什么版本的解析器解析
	<?xml version="1.0" ?>

	encoding : 解析xml中的文字的时候,使用什么编码来翻译
	<?xml version="1.0" encoding="gbk" ?>

	standalone  : no - 该文档会依赖关联其他文档 ,  yes-- 这是一个独立的文档
	<?xml version="1.0" encoding="gbk" standalone="no" ?>

###encoding Detailed

When parsing this xml, what encoding is used for parsing. -decoding.

 文字, 而是存储这些文字对应的二进制 。 那么这些文字对应的二进制到底是多少呢? 根据文件使用的编码 来得到。 

The default file is saved in GBK encoding.

So if we want our xml to display Chinese normally, there are two solutions

  1. Let encoding also be GBK or gb2312.

  2. If encoding is utf-8, utf-8 must also be used when saving the file

  3. The ANSI we saw when we saved it actually corresponds to our local encoding GBK.

For general purpose, it is recommended to use UTF-8 encoding to save, and the encoding is utf-8

###Element definition (label)

  1. In fact, they are the tags inside, and the ones enclosed in <> are called elements. Come in pairs. as follows:

    <stu> </stu>
    
  2. The first element declared in the document is called the root element (root tag)

  3. Tags can be nested inside tags

  4. Empty label

    既是开始也是结束。 一般配合属性来用。
    
    <age/>
    
    
    <stu>
    	<name>张三</name>
    	<age/>
    </stu>
    
  5. The label can be customized.

    XML naming rules
    XML elements must follow the following naming rules:

    The name can contain letters, numbers and other characters. The
    name cannot start with numbers or punctuation. The
    name cannot start with the characters "xml" (or XML, Xml). The
    name cannot contain spaces.

    The naming is as simple as possible, so that the name is known

###Simple Elements & Complex Elements

  • Simple element

The element contains ordinary text

  • Complex elements

Other elements can be nested inside the element

###Attribute definition

Defined in the element, <element name attribute name="attribute value"></element name>


Zhang San
18


Li Si
28

##xml Comments:

Same as html comments.

<!-- --> 
如: 

	<?xml version="1.0" encoding="UTF-8"?>
	<!-- 
		//这里有两个学生
		//一个学生,名字叫张三, 年龄18岁, 学号:10086
		//另外一个学生叫李四  。。。
	 -->

XML comments are not allowed to be placed in the first line of the document. Must be below the document statement.

##CDATA区

  • invalid symbol

    Strictly speaking, only the characters "<" and "&" are illegal in XML. Ellipses, quotation marks, and greater than signs are legal, but it is a good practice to replace them with entity references.

    < <
    & &

If there are too many characters in a certain string and it contains text like tags or keywords, you don't want the xml parser to parse it. Then you can use CDATA to come! [img](file:///E:/%E9%BB%91%E9%A9%AC_%E5%B0%B1%E4%B8%9A%E7%8F%AD/% E6%96%87%E4%BB%B6%E8%B5%84%E6%96%99/%E8%AF%BE%E4%BB%B6%E8%B5%84%E6%96%99/% E7%AC%AC%E4%B8%80%E9%98%B6%E6%AE%B5_web%E5%9F%BA%E7%A1%80/day09_XML&tomcat/day09/code&%E8%B5%84%E6% 96%99/%E7%AC%94%E8%AE%B0/img/parse_type.png?lastModify=1496374403) packaging. However, this CDATA is generally seldom seen. Usually when the server returns data to the client.

<des><![CDATA[<a href="http://www.baidu.com">我爱黑马训练营</a>]]></des>

##XML parsing

In fact, it is to get the character data or attribute data in the element.

###XML parsing method (frequently asked interviews)

There are many kinds, but there are two commonly used ones.

  • JUDGMENT

  • SAX

icon

###API for these two parsing methods

For some organizations or companies, what solutions do they provide for the above two analysis methods?

	jaxp  sun公司。 比较繁琐

	jdom
	dom4j  使用比较广泛

###Dom4j basic usage

	element.element("stu") : 返回该元素下的第一个stu元素
	element.elements(); 返回该元素下的所有子元素。 
  1. Create SaxReader object

  2. Specify the parsed xml

  3. Get the root element.

  4. Get child elements or descendant elements below according to the root element

     try {
     	//1. 创建sax读取对象
     	SAXReader reader = new SAXReader(); //jdbc -- classloader
     	//2. 指定解析的xml源
     	Document  document  = reader.read(new File("src/xml/stus.xml"));
     	
     	//3. 得到元素、
     	//得到根元素
     	Element rootElement= document.getRootElement();
     	
     	//获取根元素下面的子元素 age
     //rootElement.element("age") 
     	//System.out.println(rootElement.element("stu").element("age").getText());
    
    
     	//获取根元素下面的所有子元素 。 stu元素
     	List<Element> elements = rootElement.elements();
     	//遍历所有的stu元素
     	for (Element element : elements) {
     		//获取stu元素下面的name元素
     		String name = element.element("name").getText();
     		String age = element.element("age").getText();
     		String address = element.element("address").getText();
     		System.out.println("name="+name+"==age+"+age+"==address="+address);
     	}
     	
     } catch (Exception e) {
     	e.printStackTrace();
     }
    

SaxReader creates the object.

Document
Element

  1. See documentation

  2. Remember the keywords.

  3. Click on an object first.

  4. Take a look at the return value of the method.

  5. According to the usual accumulation. getXXX setXXX

###Dom4j's Xpath usage

Xpath is supported in dom4j. XPath is actually the path language of xml, which allows us to quickly locate a specific element when parsing xml.

  1. Add jar package dependency

    jaxen-1.1-beta-6.jar

  2. When searching for the specified node, search according to XPath syntax rules

  3. The subsequent code is the same as the previous parsing code.

     	//要想使用Xpath, 还得添加支持的jar 获取的是第一个 只返回一个。 
     	Element nameElement = (Element) rootElement.selectSingleNode("//name");
     	System.out.println(nameElement.getText());
    
    
     	System.out.println("----------------");
    
     	//获取文档里面的所有name元素 
     	List<Element> list = rootElement.selectNodes("//name");
     	for (Element element : list) {
     		System.out.println(element.getText());
     	}
    

##XML Constraints【Understand】

In the following document, the ID value of the attribute is the same. This is impossible in life. And there are several names of the second student. Generally very few. So how to specify that the ID value is unique, or that the element can only appear once, not multiple times? It is even stipulated that only specific element names can appear in it.

	<stus>
		<stu id="10086">
			<name>张三</name>
			<age>18</age>
			<address>深圳</address>
		</stu>
		<stu id="10086">
			<name>李四</name>
			<name>李五</name>
			<name>李六</name>
			<age>28</age>
			<address>北京</address>
		</stu>
	</stus>

###DTD

语法自成一派, 早起就出现的。 可读性比较差。 
  1. Introduce DTD on the network

    1. Introduce local DTD

  2. Embed DTD constraint rules directly in XML

     <!ELEMENT stu (name,age)>
     <!ELEMENT name (#PCDATA)>
     <!ELEMENT age (#PCDATA)>
    

    ]>

    Zhang San 18
     <!ELEMENT stus (stu)>  : stus 下面有一个元素 stu  , 但是只有一个
     <!ELEMENT stu (name , age)>  stu下面有两个元素 name  ,age  顺序必须name-age
     <!ELEMENT name (#PCDATA)> 
     <!ELEMENT age (#PCDATA)>
     <!ATTLIST stu id CDATA #IMPLIED> stu有一个属性 文本类型, 该属性可有可无
    
    
     元素的个数:
    
     	+ 一个或多个
     	*  零个或多个
     	? 零个或一个
    
     属性的类型定义 
    
     	CDATA : 属性是普通文字
     	ID : 属性的值必须唯一
    
    
     <!ELEMENT stu (name , age)>		按照顺序来 
    
     <!ELEMENT stu (name | age)>   两个中只能包含一个子元素
    

###Schema

其实就是一个xml , 使用xml的语法规则, xml解析器解析起来比较方便 , 是为了替代DTD 。
但是Schema 约束文本内容比DTD的内容还要多。 所以目前也没有真正意义上的替代DTD


约束文档:
	<!-- xmlns  :  xml namespace : 名称空间 /  命名空间
	targetNamespace :  目标名称空间 。 下面定义的那些元素都与这个名称空间绑定上。 
	elementFormDefault : 元素的格式化情况。  -->
	<schema xmlns="http://www.w3.org/2001/XMLSchema" 
		targetNamespace="http://www.itheima.com/teacher" 
		elementFormDefault="qualified">
		
		<element name="teachers">
			<complexType>
				<sequence maxOccurs="unbounded">
					<!-- 这是一个复杂元素 -->
					<element name="teacher">
						<complexType>
							<sequence>
								<!-- 以下两个是简单元素 -->
								<element name="name" type="string"></element>
								<element name="age" type="int"></element>
							</sequence>
						</complexType>
					</element>
				</sequence>
			</complexType>
		</element>
	</schema>

实例文档:
	<?xml version="1.0" encoding="UTF-8"?>
	<!-- xmlns:xsi : 这里必须是这样的写法,也就是这个值已经固定了。
	xmlns : 这里是名称空间,也固定了,写的是schema里面的顶部目标名称空间
	xsi:schemaLocation : 有两段: 前半段是名称空间,也是目标空间的值 , 后面是约束文档的路径。
	 -->
	<teachers
		xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		xmlns="http://www.itheima.com/teacher"
		xsi:schemaLocation="http://www.itheima.com/teacher teacher.xsd"
	>
		<teacher>
			<name>zhangsan</name>
			<age>19</age>
		</teacher>
		<teacher>
			<name>lisi</name>
			<age>29</age>
		</teacher>
		<teacher>
			<name>lisi</name>
			<age>29</age>
		</teacher>
	</teachers>

##The role of namespaces

If an xml wants to specify its constraint rules, assuming that a DTD is used, then this xml can only specify one DTD, not multiple DTDs. But if a xml constraint is defined in the schema, and there are multiple schemas, then it is ok. Simply put: an xml can refer to multiple schema constraints. But only one DTD constraint can be referenced.

The role of the namespace is to specify which set of constraint rules the element uses when writing an element. By default, if there is only one set of rules, then all can be written like this

<name>张三</name>

<aa:name></aa:name>
<bb:name></bb:name>

###Program Architecture

Web games

  • C/S(client/server)

QQ WeChat LOL

advantage:

有一部分代码写在客户端, 用户体验比较好。 

Disadvantages:

服务器更新,客户端也要随着更新。 占用资源大。 
  • B/S(browser/server)

Web games, WebQQ...

advantage:

客户端只要有浏览器就可以了。 	占用资源小, 不用更新。 

Disadvantages:

用户体验不佳。 

###server

In fact, the server is a computer. The configuration is better than normal.

###Web server software

The client enters the address in the address bar of the browser, and the web server software receives the request, and then responds to the message.
Process the client's request and return resources | information

Web applications need server support. index.html

Tomcat  apache

WebLogic BEA
Websphere IBM  

IIS   微软

###Tomcat installation

  1. Unzip it directly and find bin/startup.bat

  2. Can be installed

After startup, if you can see the black window normally, it means the installation has been successful. In order to be foolproof, it is best to enter: http://localhost:8080 in the address bar of the browser , if you see the content, it means you are successful.

  1. If you double-click startup.bat, you see a flashing situation, usually because the environment variables of the JDK are not configured.

###Tomcat catalog introduction

bin##

	> 包含了一些jar ,  bat文件 。  startup.bat

## conf Tomcat configuration server.xml web.xml

lib

  	tomcat运行所需的jar文件

logs

	运行的日志文件

temp

	临时文件

webapps##

	发布到tomcat服务器上的项目,就存放在这个目录。	

work (currently don't care)

	jsp翻译成class文件存放地

##How to publish a project to tomcat

Requirements: How to make other computers access the resources on my computer. stu.xml

localhost : 本机地址

###1. Copy this file to webapps/ROOT and access it in the browser:

	http://localhost:8080/stu.xml

* 在webaps下面新建一个文件夹xml  , 然后拷贝文件放置到这个文件夹中


http://localhost:8080/xml/stu.xml

	http://localhost:8080 : 其实对应的是到webapps/root
	http://localhost:8080/xml/ : 对应是 webapps/xml

	使用IP地址访问:

	http://192.168.37.48:8080/xml/stu.xml

###2. Configure virtual path

Use localhost:8080 to open the tomcat homepage, find the tomcat document entry on the left, click it, and then find the Context entry on the left, click to enter.

http://localhost:8080/docs/config/context.html
  1. Find the host element node in conf/server.xml.

  2. Add the following content.

     <!-- docBase :  项目的路径地址 如: D:\xml02\person.xml
     path : 对应的虚拟路径 一定要以/打头。
     对应的访问方式为: http://localhost:8080/a/person.xml -->
     <Context docBase="D:\xml02" path="/a"></Context>
    
  3. Enter in the browser address bar: http://localhost:8080/a/person.xml

###3. Configure virtual path

  1. Create a new xml file under the tomcat/conf/catalina/localhost/ folder, the name can be defined by yourself. person.xml

  2. Write the following in this file

    <?xml version='1.0' encoding='utf-8'?>

  3. Visit on the browser

    http://localhost:8080/person/xml name can

###Configure Tomcat for Eclipse

  1. Right-click in the server to create a new server, select the apache category, find the corresponding tomcat version, and then configure it step by step.

  2. After the configuration is complete, in the server, right-click the server just now, then open, find the Server Location above, and select Use Tomcat installation…

  3. Create a web project, define an html file under WebContent, right-click the project, and run as server

##to sum up:

xml

	1. 会定义xml

	2. 会解析xml

		dom4j  基本解析

		Xpath手法


tomcat

	1. 会安装 ,会启动 , 会访问。

	2. 会设置虚拟路径

	3. 给eclipse配置tomcat

Guess you like

Origin blog.csdn.net/qq_37040173/article/details/89335775