Today's content
1.xml Introduction
2.xml constraints
3.xml parsing
The above content can understand, understand
4. Snake (Supplement)
------------------------------------------------
1.xml
XML: Extensible Markup Language.
XML function: Used to store and transfer data.
The difference between XML and HTML
? XML is not an alternative to HTML. -----XML corresponds to XLST equivalent to CSS in HTML
? XML and HTML are designed for different purposes:
? XML is designed to transmit and store data, with the focus on the content of the data.
? HTML is designed to display data with the focus on the appearance of the data.
? HTML is designed to display information, while XML is designed to transmit information.
XML its markup is not pre-defined
2. About the XML writing specification. Its components
For a standard XML it consists of the following parts
Document Declaration
Elements
Property
Comments
CDATA zones, special characters
Processing instructions (processing instruction)
1. Document Declaration
Function: Used to declare that it is currently an XML document.
notation: <?xml version= "1.0" encoding= "Utf-8"?>
The above is a document declaration, which serves to inform the current XML document.
Version is a property note: In XML it requires strict syntax for all attributes to be quoted.
It can only fetch 1.0 for the value of version
Encoding: It represents the encoding of the information in the current XML document. Common values are GBK iso8859-1 gb18030 bg2312 utf-8
Standalone: Its role is to describe whether the current document is a separate document. This property is seldom used by us.
XML documents can also be parsed directly by the browser.
Considerations for Document Declarations:
1. The document itself is encoded in the same way as the encoding encoding.
2. All attributes are enclosed in quotation marks.
3. About full-width whitespace issues.
2. Element--refers to the label in our XML.
Elements in 1.xml (that is, labels) must have an end. (Can be self-closing)
Labels in 2.xml can be nested, but not cross-nested.
3. A standard XML with only one root element. *********************************
4. The carriage return in the XML is the element that exists as a line break.
About the naming conventions for elements
Case sensitivity, such as,<p> and <p>, are two different tokens.
You cannot start with a number or a "_" (underscore).
You cannot start with XML (or XML, or XML, and so on).
Cannot contain spaces.
The middle of the name cannot contain a colon (:). ---Schema is used in its name: to make a special definition.
-----------------------------
3. Properties
1. Attribute values must be caused by single or double quotation marks.
2. Use child elements to describe information as much as possible, without using attributes.
4. Notes
<!--
-
As with HTML.
5.CDATA area.
The contents of the CDATA area are displayed as is, that is, not parsed by the browser.
Wording: <! [cdata[
Content
]]>
When to use: we have some information in the document that does not require browser parsing, and the direct output allows you to place the information in a CDATA area.
If there are only a few content in the document that need to be output as is, you can also use the literal characters.
6. Instruction (PI)
<?xml-stylesheet type= "Text/css" href= "1.css"?>
XML Summary:
All XML elements must have a close tag
The XML tag is case sensitive
XML must be correctly nested in order
The XML document must have a root element
Attribute values for XML must be quoted
Special characters must be escaped
Spaces in the XML are preserved
-----------------------------------------------------------------
XML constraints
Constraints: XML documents can be written in what cannot be written.
There are two types of XML constraints
The DTD------STRUTS2 framework, which uses DTDs as a constraint.
Schema-----is used as a constraint in the hibernate,spring behind.
The DTD is simple--older.
Schema---more complex now, it is common to use schema to constrain XML in development.
Dtd
Quick Start:
1.DTD constrained file with the suffix name DTD.
2. Importing DTDs using DOCTYPE
Question: How can the current DTD be judged to constrain the XML file, and the constraint succeeds.
1. In development, we can use the IDE directly for constraint detection.
2. You can use JS directly to check the XML document if it follows the DTD constraint.
------------------------------------------
The 1.DTD is associated with XML in three different ways.
1. Internal DTD
is to write the DTD constraint directly in the XML file.
<?xml version= "1.0" encoding= "UTF-8" standalone= "yes"?>
<! DOCTYPE Bookshelf [
<! ELEMENT Bookshelf (book +) >
<! ELEMENT Book (title, author, price) >
<! ELEMENT title (#PCDATA) >
<! ELEMENT author (#PCDATA) >
<! ELEMENT Price (#PCDATA) >
]>
< bookshelf >
< books >
< title >java Employment Training Tutorial </title >
< author > Zhang Xiaoxiang </author >
< price >39.00 Yuan </price >
</book >
...
</Bookshelf >
2. External DTD
The DTD file is a separate file.
1. Local DTD
<! DOCTYPE Document root node SYSTEM "DTD file URL" >
2. DTD on the network
<! DOCTYPE Document root node Public "DTD name" "DTD file URL" >
2. Learning DTD syntax
1. Elements
How to declare an element:
<! The content type of the element element's name >
<! ELEMENT Books (book+) > Description The book under books can appear one or more times.
The content type of the element
1.PCDATA: Text message
2.EMPTY: Empty
3.ANY: It can be text or child elements.
The place to note about the element's writing.
1.<! ELEMENT Books (book+) > There must be a space between books and (book+).
2. for (#PCDATA) parentheses must be added if empty any
2. Properties
Property Definition Format
<! attlist element name Attribute name type constraint >
Property Type: CDATA enumeration ID
CDATA represents a string.
Enumeration (En1|en2|en3 ...) represents a property value that must be selected from.
The ID attribute value is unique.
Attribute constraints:
#REQUIRED necessary.
#FIXED bind a value.
#默认值
Example
<attlist book lang CDATA #REQUIRED > lang attribute must be.
<attlist book Lang (en|zh) #REQUIRED > lang attribute must be, only between zh and en value.
<attlist book Lang CDATA #FIXED the "en" > Lang attributes are already bound values, the value can only be en.
The <attlist book lang ID #REQUIRED > lang attribute must have and be unique.
<attlist book Lang CDATA "zh" > lang attribute if not written, the default value is zh, which writes the value of the property that is written.
3. Entities
What is an entity:
Is that we have some duplicate information in our XML or DTD file that we can declare as an instance and use the entity directly when it is used.
The entity is divided into two types
1. Referencing an entity---used in an XML document
2. The parameter entity---used in the DTD document.
1. Referencing entities:
Syntax format:
<! Entity entity name "entities Content";: Turn directly into entity content
Citation method:
& Entity name;
The parameter entity must be used in an external DTD file and cannot be used in an internal DTD
2. Parametric entities
Syntax formatting:
<! Entities% entity name "entity content";: Turn directly into entity content
Citation method
% entity name;
----------------------------------------------------------------------------------
SCHEMA
It is also a way of constraining XML files.
What is the difference between a DTD and schema?
The 1.DTD document itself has its own syntax rules.
The schema essence is an XML file that complies with the XML syntax.
When a 2.DTD document constrains XML, the data type cannot be explicitly defined.
Schema it can accurately describe the data type.
The suffix of the 3.DTD file is the DTD.
The suffix of the schema file is XSD.
Model Document---XSD
Instance Document---XML
Important Concept: namespaces
QuickStart---Purpose: Understanding namespaces
1. Make an XML file. Books.xml
<books>
<book>
<name>java Programming Ideas </name>
<price>99</price>
<author>tom</author>
</book>
</books>
2. Define a Schema constraint file. (Suffix is XSD)
Let it automatically create schemas in the IDE.
3. Learn about the relationship between defined elements and elements in the schema;
For an element that can contain elements-----complex type
<element name= "Books" >
<complexType><!--used to define a complex element--
<sequence> <!--Set Order--
<element name= "book" >
<complexType>
<sequence>
<element name= "Name" type= "string"/>
<element name= "Price" type= "Double"/>
<element name= "Author" type= "string"/>
</sequence>
</complexType>
</element>
</sequence>
</complexType>
</element>
4. Associate our schema constraint file with the XML file.
----------------------------------------------------------------------------
Review:
1.xml
Component: 1. Document declaration 2. Element 3. Property 4. Note 5.CDATA 6. directive.
2. Constraints
1.DTD
Three ways to import
1. Internal
2. External
1. Local SYSTEM
2. Network public
2.DTD----Element Attribute entity
3.SCHEMA----namespaces
It is used to define a name and give us an XSD file where it can be suspended. This is a virtual name,
In general, we use the domain name (http;//www.itcast.cn), in the XML file can import our namespaces,
and find our XSD file under the main space. This allows the XML to be constrained.
Schema differs from DTD.
----------------------------------------------------------------
About schema syntax
1. All schema documents (XSD files), their root elements are all schema
2.targetNamespace---for defining namespaces
3.xmlns---for introducing a namespace
In xmlns: name
This name is a prefix that can help you differentiate between multiple schemas in an XML file.
4.elementFormDefault is used to control when a prefix can be written.
Unqualified: The root element is prefixed, and the child element can be added without
Qualified: The root element is prefixed, and the child element must be added.
About defining attributes in the schema file.
Defining element elements
The MaxOccurs property can define the number of child elements.
--------------------------------------------------------
Defines two XSD files that are imported in an XML file.
Books.xml
Books.xsd---Declare the price element in this XSD in price.xsd.
Price.xsd---it inside declares a price element, which requires that the value must be between 50-100.
-------------------------------------------------------------------------------------
About parsing an XML file.
JavaScript, the XML is parsed by JS, and the document object is generated.
Parsing via JS is a client-side operation.
The XML is parsed on the server side through Java code.
Parsing an XML----is a read-write operation of an XML file. (We are not using IO)
about how to parse XML files in Java (way)
There are two ways of parsing XML
1.dom
2.sax
3.pull
What is the difference between DOM and sax parsing?
What is DOM parsing: loading the entire XML document into the JVM memory, generating a DOM tree. We can easily read and write XML.
What is Sax parsing: This approach is event-driven. Reads a row and parses a row. Therefore, Sax parsing can only be read, not write operations.
Concrete implementation of XML parsing
JAXP (SUN)---Sum standard parsing method Dom Sax parsing does not need to import any jar package JDK contains API
Here are two ways to import third-party jar packages If you want to use them
Jdom
DOM4J (emphasis)---apply more struts2 hibernate spring in the framework
After hibernate4, the XML parsing adopts XStream. (Will speak later)
----------------------------------------------------------------------------------------------
Analysis of JAXP:
The JAXP Development Kit is part of the J2SE, which consists of Javax.xml, Org.w3c.dom, org.xml.sax packages and their child packages
For the DOM tree, all nodes of the node Element Text Attr Document
1.dom parsing.
Dom parsing of books.xml files
Steps:
1.DocumentBuilderFactory---About the DOM parsing factory, you get this factory and you get the parser.
2.DocumentBuilder---Parser class, which can get the document object of an XML file through the parser class.
The 3.Document represents the entire document, which is actually the DOM tree, and we get the tree to manipulate any node in the tree.
API used after getting the document object
1.getElementsByTagName (String name); Gets the label by name.
2.getTextContent (); Gets the text information in the label.
NodeList
1.getLength () Get length
2.Node Item (int index) Gets the first item.
3.getChildNodes () Gets all child nodes.
4.getNodeName () Getnodetype () Getnodevalue ();
Question: How do you iterate through all the content in the entire document?
You need to use recursion, how to decide whether to end. HasChildNodes () This method can be used to determine whether a child element is available.
If there are child elements, use Getchildnodes () to get,
Action extension: Complete the traversal of an XML file.
--------------------------------------------------------------------------------------
5.getAttribute () Get Properties
6. About the JAXP create operation
Create element-----document.createelement ("name")
Create Attribute---
Create text-------element.settextcontent ("TextValue");
7. Modifications
8. Delete
Getparentnode () Gets the parent node.
RemoveChild () Deletes a child node. Note that you want to work with the parent node.
RemoveAttribute ();---delete attribute operation.
---------------------------------------------------------------------------------
2.sax Parsing (understanding)
It can only read and not write.
It is an event-driven approach.
XML Introduction to Constraint parsing