XML Basic Knowledge-->spring configuration

Source: Internet
Author: User
Tags cdata format definition xml parser

special characters FOR XML

There are 5 special characters in the XML, namely,:&<> "'. If the injected value in the configuration file includes these special characters, special processing is required. There are two workarounds: first, the <! in this example [cdata[]]> Special tags, wrapping strings containing special characters, and second, using XML escape sequences to represent these special characters, the corresponding XML escape sequences for these 5 special characters are described in table 4-2:



When spring is in the XML configuration, if the property value contains a special symbol for the XML, we deliberately add a <! outside the property value. [cdata[]]> 's XML special handling tag, <! [cdata[]]> 's role is to have the XML parser treat the string in the tag as plain text to prevent some strings from damaging the XML format. Take a look at an example:

XML code
  1. <Bean id="car" class="Com.baobaotao.attr.Car">
  2. <property name="Maxspeed">
  3. <value>200</value>
  4. </Property>
  5. <property name="brand">①
  6. <value><![ cdata[Hongqi &CA72]]></value>
  7. </Property>
  8. </Bean>



If you use an XML escape sequence, we can replace the configuration in Listing 4-10 with the following configuration:

XML code
    1. <property  name= "brand" >< value> Hongqi &amp; Ca72</value></property>  



Declaration of the schema namespace

For XML-based configuration, Spring 1.0 's configuration file is in DTD format, and Spring 2.0 uses the schema format later, which allows different types of configurations to have their own namespaces, making the configuration file more extensible. In addition, the spring-based schema configuration scheme provides a simplified configuration method for many areas of the problem, so configuration work is greatly simplified.

Taking a schema-based configuration format, the file header declaration can be a bit more complex, first look at a simple example:



To understand what is declared in the file header, you need to learn a little bit about XML schema, which declares the namespace in the document through xmlns in the document root node. We have defined 3 namespaces in the code above:

① Default namespace: It does not have a namespace name and is used for the definition of the spring bean;
②xsi namespace: This namespace is used to specify the appropriate schema style file for each namespace in the document, which is the standard namespace defined by the standard organization;
③AOP namespace: This namespace is Spring's namespace for configuring AOP, and is a user-defined namespace.

The definition of a namespace is divided into two steps: The first step specifies the name of the namespace, and the second step specifies the location of the schema document style file for the namespace, separated by a space or carriage return line.

In the first step, you need to specify the name and the name of the namespace, see the following configuration for the namespace defined:

XML code
    1. xmlns:aop="HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"



AOP is a namespace alias, typically with a simple and easy-to-remember name, and the elements behind the document can be distinguished by namespace aliases, such as <aop:config/>. HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP is the fully qualified name of the space, and is used to the official website of the document publisher and the relevant website directory as the fully qualified name. This naming method can identify the organization that the document belongs to, and can avoid the problem of duplicate name very well. However, from the XML Schema syntax, aliases and fully qualified names can be arbitrarily named.

If the namespace alias is empty, the namespace is the document default namespace, and the element with no namespace prefix in the document belongs to the default namespace, such as <beans/>, <bean/>, and so on, which belong to the default namespace defined at ①.
In the second step, the corresponding schema document format definition file is specified for each namespace, and the syntax for the definition is:



Namespaces use fully qualified names, and after each organization publishes the schema file, it provides a reference URL address for the schema file, which is typically used to specify the schema file for the namespace. The namespace name and the corresponding schema file address are separated by a space or carriage return, and this separation method is also used between different namespaces.

The schema file address for the specified namespace has two uses: The XML parser can obtain the schema file and validate the document in a format, and in the development environment, the IDE can refer to the schema file to provide guidance for document editing.

These articles are excerpted from my "Spring 3.x enterprise Application Development Practice", I will be in the form of a serial, in succession here. You are welcome to discuss.

XML Basic Knowledge-->spring configuration

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.