Java Web Learning notes-xml & DTD & Schema

Source: Internet
Author: User
Tags processing instruction xml parser

Development Tools
JUnit unit test
Common applications of XML files:
1. Data Exchange
2. Configuration File
1. xml Overview
1.1xml concept: XML is essentially a data storage format. It can store the relationship between data while saving data. XML uses tags to store data content, and uses nested relationships between tags to store relationships between data.
1.2xml application scenarios
1.2.1 data transmission: XML is essentially a string with cross-platform features. It is supported by all development languages and operating systems. XML can be used for cross-platform and cross-language data transmission.
1.2.2 used as a configuration file: XML can also store hierarchical relationships between data while saving data content. XML is always used as a configuration file.
1.3xml file: Write Data in XML format to a file. Such a file is an XML file. XML is used as the suffix name. Note that XML can be understood as an XML file in a narrow sense. XML can exist in other forms (in memory or transmitted over the network) in addition to the file format ).
1.4xml verification: in addition to the built-in HTML Parser, the browser also has the built-in XML parser. With this, we can use the browser to verify the XML. If XML complies with the specification, the browser will open normally. If the XML does not comply with the specification, an error message and the number of error lines will be prompted.

2. XML syntax
2.1 Document declaration: tells the parser how to parse the current XML, which is a declaration for the current XML. The document declaration must appear in the first line of XML and can only appear once. An XML file that does not contain the Document declaration is called a poorly formatted XML file. Although many Resolvers can parse it, it is not compliant with the specifications and is risky. Therefore, we do not recommend this method.

2.1.1 <? XML version = "1.0"?> Note: All question marks and spaces must be half-width English characters. version indicates the standard followed by the current XML. Up to now, the 1.0 standard is used.
2.1.2 <? XML version = "1.0" encoding = "gb2312"?> The encoding attribute specifies the sequence set used by the current XML. The parser uses the sequence set to decode the XML. If the encoding used for saving an XML file is different from the encoding specified by encoding, garbled characters may occur.
2.1.3 <? XML version = "1.0" standalone = "no"?> The standalone attribute declares whether the current document is an independent document. The default value is yes. If the document depends on other documents, set this attribute to No. Many parsing engines do not really refer to this attribute, but standards are standards. We need to learn according to standards.

Elements 2.2
A tag is an XML element.
2.2.1 A tag can start or end a tag. the text between the start tag and the end tag is called the TAG body. If a tag does not contain the TAG body or other elements, you can merge the start tag and end tag. Such a tag is called an auto-closed tag.
Example: <A> I am a label </a> <A/>
2.2.2 labels can be nested with sublabels. Note that all nesting operations must be reasonably nested.
2.2.3 A Well-formatted XML file must contain only one root tag.
2.2.4xml element naming rules:
Case Sensitive. For example, <p> and <p> are two different tags.
It cannot start with a number or underscore.
It cannot start with XML (or XML, or XML.
It cannot contain spaces.
The name cannot contain a colon (:).
2.3 attributes
2.3.1 an XML element can contain multiple attributes. One attribute is divided into the attribute name and attribute value. The attribute value should be enclosed in double quotation marks or single quotation marks.
2.3.2 In fact, the attribute of an element can be converted to the sub-tag of the element, which is effective in the content.
Example <China captital = "Beijing"/>
--->
<China>
<Capital>
Beijing
</Capital>
</China>
2.3.3 attributes must also follow the same naming rules as elements.
2.4 annotations
<! -- Comment content -->
2.4.1 watching cannot appear before the document declaration.
2.4.2 no nested comments can be added during watching
2.5cdata: The content contained in the CDATA area will not be parsed by the parser, but will be processed by the composition.
<! [CDATA [
Escape content
]>
2.6 escape characters
Escape characters are used to escape special characters in XML.
& -- "& Amp;
<-- "& Lt;
> -- "& Gt;
"--" & Quot;
'-- "& Apos;
Note: The CDATA area is used to escape a piece of content. escape characters can only be used to escape a single character.
The CDATA escape is implemented by notifying the processor not to parse the content. The essence of the escape is the special character. Escape characters are used to replace special characters with other characters. After the characters are parsed, they are displayed with special characters. In fact, escape characters are transmitted behind the characters.

2.7 processing commands
The method used to notify the parser to parse XML
<? XML-stylesheet type = "text/CSS" href = "1.css"?>
In fact, the Document declaration is also a special Processing Instruction
Learn more: in fact, the purpose of XML was to replace HTML for Web pages. As a result, this guy is too complicated. It is too complicated for the front-end developers, so it has not been implemented yet, but it is used for configuration files and data transmission.


3. xml constraint Technology
The XML constraint technology provides a document to constrain the writing of an XML document.
The two most common XML constraint technologies on the market are DTD schema, both of which are standards and are released by W3C organizations.

3.1dtd

3.1.1 how to introduce DTD in XML
(1) External import: The DTD constraint is written in an external file and then introduced in XML.
<1> introduction of DTD files on the local hard disk
<! Doctype root element name system "DTD file path">
Example: <! Doctype bookshelf System "book. DTD">
<2> introduce the DTD on the public location
<! Doctype root element name public "DTD name" "dtd url">
Example: <! Doctype web-app public
"-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>
(2) Internal Introduction: The DTD constraint is used to directly uninstall an XML file.
Example:
<! Doctype bookshelf [
<! Element bookshelf (Book +)>
<! Element book (title, author, price)>
<! Element name (# pcdata)>
<! Element author (# pcdata)>
<! Price of element (# pcdata)>
]>
3.1.2 how to constrain XML elements
<! Element element name element constraint>
Element constraints:
(1) element content: the element content must be enclosed in parentheses.
<! Element bookshelf (...)>
",": If the content in the brackets is separated by commas, it indicates that the content must appear in order.
"|": If the content in the brackets is separated by a vertical line, it indicates that only one of the content can appear.
# Pcdata: indicates that only the TAG body can be written to this element.

+: One or more (Books +)
? : 0 times or once (book ?)
*: 0 or multiple times (Book *)
 
Brackets can also be used for group operations to form more complex element constraints.
 
Example: <! Element myfile (Title *, author ?, Email) * | comment)>
(2) Storage Type
Empty: indicates that the current element cannot contain any other element.
Any: indicates that the current element can contain any element.

3.1.3 how to constrain XML attributes
<! ATTLIST element name
Property name 1 property type property constraints
Property name 2 property type property constraints
......
>
(1) attribute Constraints
# Required indicates that this attribute must exist.
# Implied indicates that this attribute is an optional attribute
# Fixed "fixed value" indicates that this attribute must take the given fixed value. If it is forcibly set to another value, the DTD verification fails. If the value parser is not set, this attribute is added by default, and set it to a given value.
"Default Value" if this attribute is not specified, the default value is used. If it is specified as another value, the specified value is used.
(2) attribute type
CDATA: indicates that the attribute value is a common text string.
Enumerated enumeration, the attribute value can only take one of the given values
Example: type (Technology | literature | Education) # required
ID: indicates that this attribute is a symbolic attribute. The value of this attribute cannot be repeated throughout the XML document. Note: The id value cannot start with a number or contain blank characters.
Example: Book ID # required


3.1.4entity (entity)
Entity: refers to the reference of a piece of content.
Entities include referenced entities and parameter entities.
(1) reference object: this reference is used in XML files.
<! Entity entity name "entity content">
Reference Method: & entity name;
Example: <! Entity Website "www.itheima.cn">
<Book type = "tech" book no. = "a111" url = "& URL;">
(2) parameter entity used in DTD
<! Entity % entity name "entity content">
Reference parameter entity: % entity name;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.