XML overview
1) What is XML?
XML, EXtend Markup Language, Extensible Markup Language
2) HTML vs XML
have been developed by the organization.
HTML syntax features: relatively loose syntax
1). Case insensitive <p> <P> <br/> <BR/>
2). The notation is not rigorous <p>xxxx</p> <p>xxxx
XML syntax, originally intended to compensate for the drawbacks of HTML
HTML function: Responsible for the structure of the Web page
XML functions:
1) Describe the relational data structure (as a configuration file for the software)
2) store data (as a small "database")
3) XML role
3.1 Describing data structures with relationships (as configuration files for software) (most scenarios)
The. properties file key value pair cannot describe the relationship
User=root
Password=root
User=root (Error description)
Password=root (Error description)
Db.xml file:
<db-list>
<db name= "MySQL";
<user>root</user>
; <password>root</p Assword>
</ Db>
<DB name= "Oracle" >
<user>admin</user>
<password>1234</password>
</db>
</db-list>
Application: Tomcat service configuration file, three frames (SSH struts2 spring hibernate)
3.2 Storing data (as a small "database")
Application: MySQL data can be used everywhere and then stored in an XML file
Third, XML syntax
XML suffix
The browser has a built-in XML parser, so you can use a browser to parse the XML file
1) XML Document declaration
Syntax: <?xml version= "1.0" encoding = "Utf-8"?>
Version:xml file version, usually 1.0
What encoding is used by the parser to parse the Encoding:xml file
Summarize:
Encoding problem in Chinese when XML file is read
1.1 Encoding when saving an XML file (save as selection encoding)
1.2 What encoding to use when parsing the parser (the encoding attribute of the XML document Declaration)
The above two coding consistency will not appear in Chinese garbled problem.
Standalone: Whether it is a standalone XML file (whether the current XML depends on other files)
2) Notes
As with HTML annotations <!---->
3) tags, attributes (emphasis)
Tag syntax:
3.1 Cannot start with a number
3.2 Strict case sensitivity
3.3 Label names do not have spaces and colons, do not use XML as a name (word of the company)
3.4 There is only one root tag (root element) in an XML file
property syntax:
3.1 attribute values must be enclosed in single or double quotation marks, cannot be mixed with single or double quotes
3.2 Multiple attributes can exist within a tag, but cannot be repeated
4) Special characters
As with HTML
Special character escaping character
< <
> >
Spaces
...
5) CDATA Block
Special characters can be processed in batches, and code in a CDATA block is not parsed into a label
6) Processing instructions (understanding)
<?xml-stylesheet type= "Text/css" href= "Person.css"?>
Tell the browser how to parse the XML file (using what style)
Can make XML into an HTML file (what style to use)
Iv. XML constrained DTD constraint (can read)--View W3cshool documentation
Two concepts differentiate:
1. Good XML file: conform to the rules of the grammar
2. Valid XML file: Compliant with developer-established constraint rules
XML Constraint classification:
DTD constraint: syntax is simple, function is not powerful
Application: Struts2, Hibernate
schema constraints: The syntax is more complex, powerful, more detailed constraints.
application: Spring Tomcat configuration
DTD constraint Syntax:
Requirements: Can read DTD constraints, write a valid XML file!!
1) How DTD is used
1.1 Internal DTD
Features: DTD content is written directly in the XML file
1.2 External local file import
<! DOCTYPE Note System "NOTE.DTD" > DTD file on the local filesystem
1.3 External Network File Import
<! DOCTYPE root element Public "name" "Http://struts.apache.org/dtds/struts-2.0.dtd" >
2) Constraint elements
<! Element name category > or <! Element elements name (element content) >
Category:
Empty: Represents a NULL element
PCDATA: Represents a normal string (does not contain child elements)
Any: Represents an element of arbitrary content (containing empty elements, including child elements)
(element content)
Order problems:
(element 1, Element 2, Element 3): element 1 must appear sequentially, element 2 appears, and element 3 appears.
Quantity problem:
Element 1: With and only 1 occurrences
Element 1*: Occurs 0 or more times
Element 1 +: appears 1 or more times
3) Constraint Properties
<! Attlist element Name Property Name property type default Value >
Default: Constraint property is required
#REQUIRED must.
#IMPLIED not necessary.
#FIXED Value of fixed value
Property type: What is the Constraint property value
CDATA Normal string
(En1|en2.) Enumeration, select one more
ID unique, attribute values must be unique in the same XML document
The DOM parsing of XML parsing
1) What is XML parsing?
Use Java code to control (manipulate: Read, modify) XML files
2) Two ways of parsing XML
Dom parsing: An object-oriented approach.
Similar to the HTML DOM.
Object:
Node Object
Document Object
Element Object
attr Object
Text Object
Comment Object
Dom Tree
Sax parsing: An event-driven approach.
3) DOM parsing tool for XML
Sun's official-JAXP tool (Java Standard)
Unofficial-Jdom Tools
Unofficial-dom4j (although not standard, easy to use!!) )
4) JAXP DOM Parsing tool
Official: Included in the JDK specification. org.w3c.dom.* API
4.1 Steps to read an XML file:
A) Create a Documentbuilderfactory object
b) Get a Documentbuilder object
c) Call the Parse method to read the XML file
4.2 Steps to write out an XML file:
A) Create a Transformfacotory object
b) Get a Transform object
c) Call the transform method to write out the contents to the XML file
Summarize:
1) JUnit tools (must be mastered)
2) XML syntax (emphasis)
3) DTD constraint (can write XML file--Manually write valid XML document according to DTD file)
4) JAXP DOM parsing API (must go back to practice)
Day05_javaee Series: XML