JavaWeb Study Notes 1: XML & Tomcat

1-XML Introduction

extendssible markup language extensible markup language
XML has the:
1, can be used to save the data
2, can be used for the profile
3, data transmission carrier

XML inverted tree structure similar to html and
specific content and functionality can be viewed api to learn, very detailed

2-XML document declaration
  1. Simple statement, version: xml parsing this time, what version of parsing
    如:<?xml version="1.0">
  2. encoding: when parsing xml text, what encoding deposit, what code resolution
    如:<?xml version="1.0" encoding="gbk">
  3. standalone: ​​no - the other documents the associated document
    如:<?xml version="1.0" encoding="gbk" standalone="no"?>
3-XML element definitions, attribute definitions

xml element definitions
Element Properties

4-XML comments, CDATA area

If you do not add a comment, the label can be placed only in order to display properly, otherwise an error. At this time can annotate solution
generally occurs in the time of data transmission, usually rare to
have two illegal characters (<&) must be replaced with an escape character
if there are too many of a certain character string, and which contains this text labels or keywords too much, do not want to parse the xml parser, you can use CDATA to wrap, but this is generally less CDATA see
用法:<![CDATA[内容]]>

5-XML parsing mode (DOM & SAX)

In fact, the element is to get inside the character data or attribute data two ways: DOM & SAX. DOM parsing to put all the documents stored in memory form a tree structure from top to bottom, there are many ways to resolve, but is commonly used in both:
DOM&SAX
the same analytical tools: jaxp, jdom, dom4j (most common)

6-XML dom4J entry

1, to create objects SAXReader
2, designated parsed XML
. 3, accesses the root element
4, obtaining sub-elements according to the root element or elements of the following descendant
element.element("stu"); //返回根元素下的第一个stu元素
element.elements("stu");//返回根元素下的所有stu元素

Specific code:

SAXReader sax = null;
Document dom = null;
try {
	String filePath = "src/xml/demo.xml";
	sax = new SAXReader();
	dom = sax.read(filePath);
	
	//返回xml文件的相对路径
	//System.out.println(dom.getName());
	
	//获得根元素对象的地址再获取根元素名
	//System.out.println(dom.getRootElement().getName());
	
	//获得Element集合(所有的stu元素),并获取元素下的数据
	List<Element> element = dom.getRootElement().elements();
	for (Element element2 : element) {
		System.out.print(element2.element("name").getText()+"\t");
		System.out.print(element2.element("age").getText()+"\t");
		System.out.print(element2.element("address").getText());
		System.out.println();
	}
} catch (Exception e) {
	e.printStackTrace();
}
7-XML Xpath entry

Encounter new knowledge, new object when:
1. 2. Note the keywords to see the document 3. There are objects to return to see what 4. 5. accumulation of normal value. ""

dom4j里面支持xpath的写法,xpath其实是xml的路径语言,支持我们在解析xml的时候,能够快速的定位判断具体的某一个元素。

  1. Add jar package depends jaxen-1.1-beta-6.jar
  2. In the Find node designated time, according to the rules of grammar to find xpath
  3. Successive code as before parsing code

Specific code:

SAXReader sax = null;
Document dom = null;
try {
	String filePath = "src/xml/demo.xml";
	sax = new SAXReader();
	dom = sax.read(filePath);
	Element re = dom.getRootElement();
	
	//要想使用Xpath,还得添加支持的jar,获取的是第一个,只返回一个
	Element nameElement = (Element)re.selectSingleNode("//name");
	System.out.println(nameElement.getText());
	
	System.out.println("--------------------");
	List<Element> list = re.selectNodes("//name");
	for (Element element : list) {
		System.out.println(element.getText());
	}
} catch (Exception e) {
	e.printStackTrace();
}
8-XML XML promise

Some of the properties in the development process and data can only be unique, to be restricted, so use restraint

  1. DTD: a constraint way, but xml parser can not be resolved, and poor readability, grammar own categories
  2. Schema: in fact, xml grammar rules, use of xml, xml parser more convenient, but more than text, so it does not replace a real sense DTD
9-XML DTD

DTD

  1. DTD introduced on the network

    Dtd dtd name on the document type root tag name dtd network path
    <! DOCTYPE stus PUBLIC "// UNKNOWN /"> "unknown.dtd">

  2. The introduction of local DTD

    Introducing local DTD: root tag name of the location of the local introduction of DTD dtd
    <DOCTYPE stus the SYSTEM "stus.dtd"!>
    DTD file code as follows:
    <stus the ELEMENT (STU)!>
    <STU the ELEMENT (name, Age, address)! >
    <! the ELEMENT name (#PCDATA)>
    <! Age the ELEMENT (#PCDATA)>
    <! address the ELEMENT (#PCDATA)>
    after the introduction, xml file stu only one, or an error

  3. Embedded directly in the XML DTD constraint rules inside

    xml document constraint rules embedded directly inside the DTD
    <! DOCTYPE stus [
    <! stus the ELEMENT (STU) +> stus element below a stu, but only a
    <! ELEMENT stu (name, age )> stu name following two elements , Age
    <! the eLEMENT name (#PCDATA)>
    <! Age the eLEMENT (#PCDATA)>
    <! ATTLIST STU ID CDATA #IMPLIED> STU has an attribute, text type, the optional attributes]>
    the number of elements: + one or more * zero or more zero or a?
    type of property: CDATA: property is regular text ID: value of the property must be unique

10-XML Schema

Schema writing, to write elements, and then gradually write level
teacher.xsd:

<!-- xmlns:xml namespace:名称空间 
targetNamespace:下面定义的元素都与这个名称空间绑定
elementFormDefault:元素的格式化情况	-->
<schema xmlns="http://www.w3.org/2001/XMLSchema" 
	targetNamespace="http://www.example.org/teacher" 
	xmlns:tns="http://www.example.org/teacher" 
	elementFormDefault="qualified">
	<!-- 为复杂元素 -->
	<element name="teachers">
		<complexType>
			<sequence>
				<!-- 为复杂元素:默认只有一个 -->
				<element name="teacher">
					<complexType>
						<sequence maxOccurs="unbounded">
							<!-- 为简单元素 -->
							<element name="name"></element>
							<element name="age"></element>
						</sequence>
					</complexType>
				</element>
			</sequence>
		</complexType>
	</element>
</schema>

teacher.xml:

<!-- xmlns:xsi:这里必须是这样的写法,也就是这个值已经固定了
xmlns:这里是名称空间,也固定了,写的是chema里面的顶部目标名称空间
xsi:schemaLocation:有两段:前半段是名称空间,也是目标空间的值,后面的约束文档的路径 -->
<teachers
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.example.org/teacher"
	xsi:schemaLocation="http://www.example.org/teacher teacher.xsd">
	<teacher>
		<name>张三</name>
		<age>28</age>
	</teacher>
</teachers>

The role of the namespace: an xml if you want to bind its constraint rules, assuming DTD, then the xml can only bind a DTD, can not bind multiple DTD, but can bind multiple Schema.

The role of the namespace binding constraints rule elements that can be used, by default, if you use a set of rules, you can write:

<name>张三</name>
<aa:name></aa:name>
<bb:name></bb:name>
11- procedural framework (BS & CS)

C / S (client / server)
  , for example: QQ micro-channel ... LOL
  advantages: a part of the code written in the client, the user experience is preferably
  disadvantages: updating the server, the client with the updated footprint large
B / S (browser / server )
  For example: web games webQQ qq-mail ...
  advantages: a client as long as the browser can be, small footprint, without updating
  Cons: poor user experience

12-Web server

Server is a computer, the configuration is better than the average
client input in the address bar of the browser address, web server software, accept the request and response
process: 1: Install the Tomcat 2: publish the project to Tomcat 3: Elipse Configuration

13-dom4J summary

When using dom4j, different levels of meaning:

Document doc = saxR.read("src/xml/teacher.xml");
System.out.println(doc.getName());	//此时还未进入xml,所以输出相对路径
Element element = doc.getRootElement();

//阶段一:
System.out.println(element.getName());	//进入xml根元素,输出根元素名
System.out.println(element.getText());	//进入xml根元素,此时两种方法一样

//阶段二:
System.out.println(element.element("teacher").getName());	//进入teacher元素,输出teacher
System.out.println(element.element("teacher").getText());	//进入teacher元素,但无法取出子元素,所以输出换行符

//阶段三:
System.out.println(element.element("teacher").element("name").getText());
HPF- self-summary

  XML & Tomcat school is in fact a long time ago, but the notes have not been made up. It also notes before, so made up to be a share, at the same time after they have forgotten where you can also refer to note here. Mainly to do a record.
  Because of their laziness, it has been a long, long time no JavaWeb continue to learn, and recently more confused, much deliberation, or pick up JavaWeb continue to learn, I will continue to learn and learning to write notes of the blog.
  --- short step, a thousand miles; not small streams into a mighty torrent.

Published 15 original articles · won praise 18 · views 4579

Guess you like

Origin blog.csdn.net/oZuoShen123/article/details/105046949