There are several days did not update the blog, the previous period of time because of the school, need to fill the tuition to continue to study in the school, delayed a few days, these two days need to supplement the previous need to learn some of the knowledge points. Start today to enter the Javaweb stage, this time we need to know some of the front-end knowledge, as well as the database, and so on, the specific I review the time to talk about, today began some basic knowledge of XML review it!!
I. XML overview
What is 1.1.xml?
Extensible Markup Language Extensible Markup Language.
Markup Language: Not only storing text content, but also storing and other information related to the content
1998 1.0
XML is a data storage format that is essentially a string.
How does XML store the data?
The XML allows the user to customize the label, which is divided into the start tag and the end tag. Other labels can be nested between the opening and closing tags.
It is the use of labels to hold data, and to use nested relationships between tags to preserve hierarchical relationships between data.
Application Scenarios for 1.2.xml
(1) Transfer data
Because XML is inherently a string that has cross-platform features, it is possible to exchange data between different systems.
(2) as a configuration file
Because XML can hold structured data, XML is often used as a configuration file for an application.
1.3.xml file
Write an XML file
1.4.xml Checksum
validating XML files
Two. Syntax
2.1. Document Declaration
Used to declare the current XML basic property information, the parser determines how the XML file is parsed according to the document Declaration.
Attention:
An XML file must contain and contain only one document declaration.
The document declaration must be placed in the first line of the XML file and cannot be preceded by any content.
If an XML file does not contain a document declaration, the XML is an XML file that does not conform to the specification.
Writing:
<?xml version= "1.0"?>
Version is used to declare the XML specification that the current XML follows, which is now 1.0
<?xml version= "1.0" encoding= "UTF-8"?>
Encoding is used to declare the character set encoding used by the current XML, and the XML parser parses the XML by parsing the XML file according to the encoding specified by the Encoding property.
Note: files are saved with the same encoding as the encoding attribute, which prevents garbled problems.
<?xml verison= "1.0" encoding= "UTF-8"? Standalone= "Yes" >
Standalone is used to declare whether the current document is independent, if the XML document does not depend on other documents, indicates that the current document is a separate document, can specify a value of Yes, and if the current document needs to rely on other documents to exist, you need to specify a value of No.
2.2. Elements
◇ a tag (tag) is an element.
◇ labels are divided into start and end tags, and the text between the opening and closing tags is called the label body.
◇ If a label does not contain a tag body and does not contain other sub-tags, you can combine the opening and closing tags into a single self-closing tag.
◇ label should be reasonable nesting, can not appear cross-nested.
◇ an XML file has and has only one root tag.
◇ naming specification for elements:
is case sensitive. such as:<p> and <p> are two different labels.
You cannot start with a number or punctuation mark.
Cannot start with XML (XML, XML).
Cannot contain spaces such as: <a b>
Cannot contain colons such as:<a:b>
easy to make mistakes:
The label does not have an end tag or is a self-closing tag without a self-closing!!!
2.3. Properties
Multiple attributes can be declared on an element, and multiple properties are separated by a space.
The attribute is concatenated with the property value by an equal sign, and the value of the property is enclosed in single or double quotation marks.
The naming and elements of attributes follow the same naming conventions.
2.4. Notes (Learn)
Format: <!--XML Comment--
Comments cannot be placed in front of document declarations
Note cannot cross-nest
2.5. Escape character (Learn)
Commonly used escape characters:
< <
> >
& &
""
' '
Three. XML constraints (Understanding)
Constraints: In XML, you can provide a constraint document to constrain the wording of an XML document
Dtd/schema
DTD: Easy to learn, quick to get started, but the function has limitations, can not limit the value of the label body type or value range
Schema: Complex, not studious, but able to achieve fine-grained qualification
1. XML Schema1.1. XML Schema Overview 1.1.1. XML Schema
XML Schema is also a schema language used to define and describe the structure and content of XML documents, which appears to overcome the limitations of DTDs
1.1.2. Schema versus DTD comparison:
The XML schema conforms to the XML syntax structure.
XML APIs such as DOM and sax can easily parse the contents of XML Schema documents.
The XML schema supports the namespace very well.
XML Schemas support more data types than XML DTDs, and enable users to customize new data types.
The ability to define constraints on XML Schemas is very powerful, and it allows you to make detailed semantic restrictions on XML instance documents.
XML Schemas cannot define entities as DTDs, more complex than DTDs, but XML schemas are now the norm for the organization, and are gradually replacing DTDs.
1.2. Schema constraint QuickStart 1.2.1. Quick Start
The XML Schema file itself is an XML file, but its extension is usually. xsd.
An XML Schema document is often called a schema document (a constraint document), and an XML file that follows this document is called an instance document.
As with an XML file, an XML Schema document must have a root node, but this root node is named schema.
After writing an XML Schema constraint document, it is often necessary to bind the elements declared in this file to a URI address, and there is a professional term in XML Schema technology that describes the process of binding elements declared in an XML Schema document to a namespace , the XML file can then be used to tell the parsing engine through this URI(the namespace) , where the elements written in the XML document come from and who is bound.
<?xml version= "1.0" encoding= "UTF-8"? ><tedu: Bookshelf xmlns:tedu= "http://www.tedu.cn" xmlns:xsi= " Http://www.w3.org/2001/XMLSchema-instance " xsi:schemalocation=" http://www.tedu.cn http://www.tedu.cn/book.xsd "> <tedu: Book > <tedu: title > Data structure </tedu: title > <tedu: Author > Min </tedu: Author > <tedu: Price >29.00 $ </tedu: Price > </tedu: Book ></tedu: Bookshelf >
<?xml version= "1.0" encoding= "UTF-8"?> <xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"targetnamespace= "http://www.tedu.cn "elementformdefault= "qualified" > <xs:element name= ' Bookshelf ' > <xs:complexType> <xs:sequen Ce maxoccurs= ' unbounded ' > <xs:element name= ' book ' > <xs:complexType> <xs:sequence> <xs:element name= ' title ' type= ' xs:string '/> <xs:element name= ' author ' type= ' xs:string '/> <xs:element name= ' price ' type= ' xs: String '/> </xs:sequence> </xs:complexType> </x s:element> </xs:sequence> </xs:complexType> </xs:element></xs:schema>
1.3. Namespace (namespace) 1.3.1. The concept of namespaces
In the XML schema, each constrained schema document can be assigned a unique namespace, represented by a unique URI (Uniform Resource Identifier, Uniform Resource Identifier). When you write a label in an XML file, you can declare from the namespace declaration (xmlns) which schema constraint document the currently-written label is from. Such as:
<tedu: Bookshelf xmlns:tedu= "http://www.tedu.cn" >
<tedu: Book >......</tedu: Book >
</tedu: Bookshelf >
Here, you use tedu to point to the name of the declaration, so that you can refer to the namespace later.
Note: The name syntax of the namespace is confusing, although the URL does not point to a file that contains the schema definition, starting with http://. In fact, this url:http://www.tedu.cn does not point to any file at all, just an assigned name.
1.3.2. XML using namespaces to introduce schemas
In order to declare the exact location of the schema file that it follows in an XML document, it is often necessary to specify it in the root node of the XML document using the schemalocation attribute, for example:
<tedu: Bookshelf xmlns:tedu= "http://www.tedu.cn"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://www.tedu.cn book.xsd" >
SchemaLocation This property has a value of two. The first value is the namespace that needs to be used. The second value is the location of the XML schema used by the namespace, separated by a space.
Note that when you use the SchemaLocation property, you also need to specify where the property comes from.
1.3.3. Using the default namespace
Basic format:
Xmlns= "URI"
Example:
< bookshelf xmlns= "http://www.tedu.cn"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://www.tedu.cn book.xsd" >
< books >
< title > Data structure </title >
< author > Min </author >
< price >29.00 Yuan </price >
</book >
< bookshelf >
1.3.4. Introducing multiple XML Schema documents using namespaces
File list: Xmlbook.xml
<?xml version= "1.0" encoding= "UTF-8"?>
< bookshelf xmlns= "Http://www.tarena.org/xmlbook/schema"
xmlns:demo= "Http://www.tarena.org/demo/schema"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.tarena.org/xmlbook/schema Http://www.tare Na.org/xmlbook.xsd
Http://www.tarena.org/demo/schema http://www.tarena.org/demo.xsd ">
< books >
< title >javascript Web Development </title >
< author > Zhang Xiaoxiang </author >
< price Demo: currency = "RMB" >28.00 yuan </price >
</book >
</Bookshelf >
1.3.5. Using namespaces to introduce XML Schema documents
File list: Xmlbook.xml
<?xml version= "1.0" encoding= "UTF-8"?>
< bookshelf xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:nonamespaceschemalocation= "Xmlbook.xsd" >
< books >
< title >javascript Web Development </title >
< author > Zhang Xiaoxiang </author >
< price >28.00 Yuan </price >
</book >
</Bookshelf >
1.3.6. Declaring namespaces in an XML Schema document
<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema"
Targetnamespace= "http://www. Tedu.cn "
elementformdefault= "qualified" >
<xs:schema>
The targetnamespace element is used to specify which namespace the element declared in the schema document belongs to.
The elementFormDefault element is used to specify whether the root element declared in the schema document and all its child elements belong to the namespace specified by targetnamespace.
1.4. Schema syntax
Slightly
Four. XML parsing
4.1. Two types of analytic thinking (!!! Important)
DOM parsing:
Adds an entire XML document to memory, using a Document object to represent the entire file. Parses all the contents of an XML document (elements, attributes, text, and so on) into one object, representing the element with an object, and representing the hierarchical relationship between the elements using the reference relationship between the object and the object. Indirectly, by manipulating objects in a program (adding and pruning) the contents of an XML document.
Advantages:
(1) can be very convenient to the node for the operation of the increase and deletion check.
(2) A document tree is saved in memory and can be reused only once.
Disadvantage:
(1) loads the entire document into memory, consumes memory space, and consumes memory if the XML document is bulky. The
(2) needs to wait for the entire XML document parsing to complete before it can operate on the node, which is relatively time-consuming and inefficient.
Package Cn.tedu.dom4j;import Java.util.iterator;import Org.dom4j.document;import org.dom4j.DocumentException; Import Org.dom4j.element;import Org.dom4j.io.saxreader;import Org.junit.test;public class Demo1 {//JUnit: Can simulate the running environment of the program , test a method @testpublic void Find () throws documentexception{//requirements: Query the title of the first book and output it to the console//1. Create a Saxreader object-the core class used to parse the XML Saxreader reader=new saxreader ();//2. Parse a xml-returns a parsed Document object document Document=reader.read ("Book.xml");//3. Returns a Rootelementelement rootele=document.getrootelement () through document,//4. Gets the iterator for all elements iterator<element> it=rootele.elementiterator ();//5. Iterate all elements while (It.hasnext ()) {element e=it.next (); Equals (E.getname ()) {iterator<element> it2= E.elementiterator ("title"); while (It2.hasnext ()) {Element e2=it2.next (); System.out.println ("Name=" +e2.getname ()); String Value=e2.gettext (); System.out.println ("value=" +value); Break;}}}}
Sax parsing:
Advantages:
(1) because it is progressive parsing, it is not necessary to load the entire XML document into memory, which consumes less memory. Theoretically, how much XML files can be parsed
(2) because it is progressive parsing, encountered the desired content can be stopped to deal with, high efficiency
Disadvantages:
(1) The data needs to be re-parsed every time.
(2) can only read the data, not the XML document can be deleted and modified operation.
4.2.DOM4J parsing
Slightly
The XML foundation of the Java Web