No nonsense xml--xml details

Source: Internet
Author: User
Tags cdata closing tag opening and closing tags processing instruction

Starting today to study XML, in fact, in the actual development, we participate in the real XML development is not a lot, write a configuration at most, but I still think it is necessary to put the knowledge of XML again. As the basic 2 kinds of data interaction vector (there is a JSON), the basic XML specification should be understood, and not simply to write a configuration to knock a few lines of code, at least should be the parsing of XML documents and Java to the processing of XML to be clear.

In addition to the above mentioned, there is actually a very common situation to use XML. For example, when we are in the code, some static final properties or fields (corresponding XML is the data) we do not want hard code to write 4 the most commonly used is 2 ways of writing in the configuration file, either the XML configuration or the properties configuration, One is written in the database, the personal feel some often change configuration these can be encapsulated as an object, and then unified in the database, such as a page of some default initialization information, and so on. So in the later time, we do not have to change the project, moving without moving, directly change the database data is good, it is convenient.


Okay, nonsense. No, no nonsense. XML now begins:
Definition: XML (extensible Markup Language) is an extensible flag language defined by World Wide Web Consortium. Extensibility refers to allowing users to customize tags (tags tags) in accordance with XML rules.
Strengths: Easily express multi-layered structure data, extensible.
Pros: Platform-independent, language-independent. The design goal is to describe the data and focus on the content of the data, separated from the display.
Reminder: You cannot use XML to write Web pages directly. Even if the XML data is included, it is still converted to HTML to be displayed on the browser.

Syntax rules: The 1,xml file has only one root tag, and the other tags must be encapsulated in the root tag, and the file's markup must form a tree structure. 2, case sensitive. 3, the attributes of the tag must be enclosed in "or".



    • XML Details:
    • I. Declaration
most XML documents begin with an XML declaration, which provides basic information about the document to the parser. The XML declaration is recommended, but it is not required.    If so, then it must be the first line of the document. For example:
<?xml  version= "1.0"  encoding= "UTF-8" standalone= "no"?>

A declaration can contain up to three name-value pairs (many call them attributes, although technically they are not). (There can be no spaces between the <?xml question mark and the XML.) )
1,version is the XML version used: 1.0, 1.1. In practice, they are usually 1.0.
2,encoding is the character set used by this document. The iso-8859-1 character set referenced in the declaration includes all the characters used in most Western European languages.
The default character is in the UTF-8 character set, which is a Unicode standard that almost supports characters and hieroglyphs in all languages of the world.
3,standalone (can be yes or no) defines whether the document is processed in isolation.      If the XML document does not reference any other files, you can specify standalone= "Yes". If the XML document references other files (such as DTDs) that describe what the document can contain, then standalone= "No". The default value is "no".
    • Second, Mark
Text between the left angle bracket "<" and the right angle bracket ">"
1. In < > called the start tag; in </>, called the closing tag
2. Empty Tags: tags that do not contain elements. Empty labels must end with "/>". Format: < empty tag name/> < empty tag name attribute list/>
Note: 1, except for empty tags, labels must be paired: Beginning and ends.     All start and end tags must match. , 2, there is no space between the marker < and the name of the tag. You can have a space or a return line before the marker "/>". 3, the label must be nested correctly.
XML tags must follow the following naming conventions:
1. The name can contain letters, numbers, and other letters or text, and can contain underscores (_), dots (.), hyphens (-)
2. The name cannot begin with a number; You can start with letters, text, or underscores.
3. The name cannot begin with the letter XML (or XML or xml:).
4. The name cannot contain spaces.
    • Iii. elements

is between the opening and closing tags. a document has only one root element. all elements under the root element are called "child elements". labels must be nested correctly. elements that do not contain a child element are called "leaves"; the element that contains the child element is called "branch".

such as: <eric> </eric>
    • Iv. attributes
The name-value pair in the start flag of an element.    All attribute values must be in single or double quotation marks.    The attributes of each element are not allowed to appear more than once. Inside the start flag, similar to the assignment statement
such as: <eric age= ">......</eric>"
    • V. Notes
Comments can appear anywhere in the document.    (It is not recommended to put it in front of the statement, some browsers will error).    Comments start with <!--and end.    Annotations cannot contain double hyphens (--), except that comments can contain anything. Any tags within the comment are ignored.
    • Vi. processing Instructions
A processing instruction is a tag that is designed to use a special code, referred to as PI.    Most XML documents begin with an XML declaration, which is itself a special processing instruction. Processing instructions encode application-specific data. A processing instruction contains a target, followed by data.    Wrap the processing instructions with the &LT,?, and?> delimiters. The target identifies the application, and the target is not recognized by the application, which ignores these processing instructions.
    • VII. Entities
The XML specification pre-defines five entities.
&lt; = = <
&gt; = = >
&quot; = = "
&apos; = = '
&amp; = = &
Custom entities: Defining <! in DTDs      Entity entity flag "solid content" >.  Referencing custom entities in XML, using & entity flags;    Represents the entity content.      In addition, characters that cannot be entered from the keyboard can use a character reference, or a character's Unicode code point to refer to the character. Start the character reference with "& #x", End with a semicolon, and x must be lowercase, using hexadecimal. such as: & #x003D;      Represents the equals sign.      You can also use character references to refer to <,>, ', ',& '. View the code point for the character (the character Map, System tools, accessories, and so on).
    • Eight, CDATA
When many entity references and character references appear in a piece of text, it can be difficult to read and write text data, and CDATA segments are introduced to solve this problem. The data section begins with the <!    [cdata[] ends with "]]>".    CDATA Everything inside will be parsed by the parser, without checking its format. However, CDATA segments cannot nest another CDATA segment.
    • Ix. Properties
property is a property of the tag, and additional information can be added to the tag.
(1) Composition of attributes
A property is a name-value pair that must consist of a name and a value, the attribute must be declared in the tag's opening tag or an empty tag, and a value is specified with "=" for the property.
The syntax is as follows:
< tag name attribute list/>
< tag name attribute list >xxx</Tag name >
For example: < table width= "height="/>
(2) The principle of having attributes
Attributes do not reflect the structure of the data, but only the additional information of the data;
A message is a property or sub-tag that is a tag, depending on the specific problem, do not break the XML data structure because of the frequent use of attributes.
Here is an XML file with a clear structure:
< building height= "23m" width= "12m" >                < structure > Concrete </structure >                < category > Commercial </category >            </building >

The following is an XML file that is not clearly structured:

As to when to use attributes and when to use child elements, it is strictly not stipulated: we are customizing an XML to follow some of the default rules, for example, if there is a next level in this attribute, then the child element should be used. The general rule is also well understood: if this field is an attached description of the previous tag, the attribute should be used. If this field has a clear parent-child inclusion relationship with the previous tag, then the child element should be used.
    • X. Name SPACE/Package
The XML file allows for custom markup, so a tag with the same name may appear, and in order to differentiate the tokens, the namespace is used.
The purpose of namespaces is to effectively differentiate between the same tokens, which are not actually present.
Syntax: Declaring a namespace with a prefix xmlns: prefix name = Name of namespace
Declaring a namespace with no prefix xmlns= the name of the namespace (default)
Note: When and only if their names are the same as two namespaces, that is, for prefixed namespaces, if the names of the two namespaces are the same, even if the prefixes are not the same, the same namespace is returned. Prefixes are just handy references.
    • XI. Custom XML
In the actual development, basically does not define an XML itself, this is only when we use the XML to practice the time will be written. We define an XML ourselves, so how do we verify that our own XML is not a problem? Very simple, open with a browser, if the browser can be opened correctly, then we have this XML is not a problem.

The following is a good-sized XML (I studied web. SPRINGMVC when I was at the earliest):

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns= "Http://java.sun.com/xml/ns/javaee" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee/http Java.sun.com/xml/ns/javaee/web-app_2_5.xsd "id=" webapp_id "version=" 2.5 "><display-name>springmvc</ display-name><!--Default Jump page Here you can configure multiple--><welcome-file-list><welcome-file>index.html</ welcome-file></welcome-file-list><!--Configure the total configuration file here the entrance to any schema is married here. Look at any project in the future to see the configuration from here to understand the framework and background technology used in the project the following 4 types of execution order is: context-param## #listener # # #filter # # #servlet-->< context-param><param-name>contextconfiglocation</param-name><!--The spring configuration file for the business layer and the persistence layer These configuration files are used by the parent class spring container If there are multiple files separated by commas--><param-value>classpath*:config/springannotation-*.xml</ param-value></context-param><!--Context Listener--><listener><listener-class> Org.springframework.web.context.contextloaderlistener</listener-class></listener><!--The Get or Load method using Hibernate is not the same as the transaction control of other methods when adding transaction control, the session is closed when the database fetches the data, so this filter is added here. To call the session open--><filter><filter-name>opensession</filter-name><filter-class> org.springframework.orm.hibernate4.support.opensessioninviewfilter</filter-class><init-param>< Param-name>sessionfactorybeanname</param-name><param-value>sessionfactory</param-value> </init-param></filter><filter-mapping><filter-name>openSession</filter-name>< url-pattern>/*</url-pattern></filter-mapping><!--Troubleshooting post submission form using AJAX technology if the Get method is a problem In general, the server is configured with the encoding format is OK, but the post is not so to customize--><filter><filter-name>encodingfilter</filter-name>< Filter-class>org.springframework.web.filter.characterencodingfilter</filter-class><init-param> <param-name>encoding</param-name><param-value>UTF-8</param-value></init-param> <init-param><param-name>forceencoding</param-name><param-value>true</param-value></init-param></ filter><!--Here is the encoding format in the JSP is set to unified--><filter-mapping><filter-name>encodingfilter</ Filter-name><url-pattern>/*</url-pattern></filter-mapping><servlet><servlet-name >springmvc</servlet-name><servlet-class>org.springframework.web.servlet.dispatcherservlet</ servlet-class><init-param><!--The location of the spring configuration file that is loaded can be combined with the general configuration file but it's best not to build a configuration file package under Src This is very clear-->< param-name>contextconfiglocation</param-name><param-value>classpath*:config/ springannotation-servlet.xml</param-value></init-param><!--value greater than 0 is the servlet that starts as soon as the container starts. 0 words are used to start it. And the smaller the number, the higher the priority of the boot order--><load-on-startup>1</load-on-startup></servlet>< Servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern> </servlet-mapping></web-app>


No nonsense xml--xml details

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.