XML of Java Learning notes

Source: Internet
Author: User
Tags cdata xml parser

XML Learning Notes

Overview of 1:xml

What is 1.1 XML?
Markup Language: <a></a> tags appear in the language
A:html Hypertext Markup Language (syntax is very strict, can not arbitrarily define the label)
B:xml Extensible Markup Language (users can define tags as they want)
< garbage ></>
Developed organization: Open source Spirit HTML XML Java Script
Version: Using 1.0

What is the use of 1.2 XML
A: Storing data
B: As a configuration file
1.3 How to use XML


The syntax of 2:xml
2.1 Document Declaration
<?xml version= "1.0" encoding= "UTF-8"?> Encode: Encoding decode: Decoding
1) Document declaration must start with <?xml and end with?>;
2) The document declaration must start at the 0-row 0-column position of the document;
3) The document declaration has only 2 properties:
A) Versioin: Specifies the XML document version. Only 1.0 is selected;
b) Encoding: Specifies how the current document is encoded when it is saved. Optional attribute, default value is UTF-8;

2.2 Elements (Element)
1. Element is the most important part of XML document, start tag element body end tag
2. The structure of ordinary elements begins with the label, element body, and end tag. For example:3. Element body: The element body can be an element, or it can be text, such as:<b><a> Hello </a></b>
4. Empty element: The element has no element body, the empty element has only the start tag, and there is no end tag, but the element must be closed by itself, for example:<c/>
5. Element naming:
A) case-sensitive
b) You cannot use a space, you cannot use a colon:
c) It is not recommended to start with XML, XML, XML
6. A well-formed XML document must have only one root element. In addition to the root element, all elements must be contained by other elements


2.3 Properties (Attribute)
1. A property is part of an element and must appear in the start tag of an element
2. Attribute definition Format: property name = attribute value, where property values must be either single or double-cited
3. An element can have a 0~n attribute, but an element cannot have the same name attribute
4. Attribute names cannot use special characters such as spaces, colons, etc., and must start with a letter


Selection of attributes and child elements
A: If the data you describe is a unique identifier (ID) for the element, it should be placed in the attribute
B: If the data you describe needs to be expanded again, it must be placed in a child element
2.4 Notes
XML comments, starting with "<!--" and ending with "--". The comment content is ignored by the XML parser!
A) Note cannot be placed on the first line of the file
b) shortcut keys for annotations
NOTES: Ctrl + SHIFT +/
Anti-Comment: Ctrl + SHIFT + \
2.4 Escape characters
? escape character
Because many symbols are already used by the XML document structure, you must use escape characters in the element body or in attribute values, such as "<", ">", "" "," "", "&".
< &lt;
> &gt;
"-&quot;
'-&apos;
& &amp
2.5 CDATA Zone
<! [cdata[
Any Content
]]>
When your tag content has a large number of escape characters, you can use CDATA extents to escape all at once


Constraints of 3:xml
XML constraints: Tags in XML can be arbitrarily extended by default, which results in the randomness of XML is too large, in order to give XML a uniform specification, you must use XML constraints

3.1 DTD constraints
DTD (document type definition), Doc type definitions
Constraints: Tag names, label attributes, label order
Our task is to define the XML file according to the DTD constraint document already provided.

3.2 DTD Constraint step
Step 1: Create the Bean-dtd.xml document and copy the "Bean.dtd" in the same directory
Step 2: Copy the required document declaration from the beginning of the DTD document
Step 3: Complete the XML content authoring

Document declaration for the 3.4 DTD
1: Internal DTD, which embeds a DTD inside an XML document, is valid only for the current XML.
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE Beans [
...//specific syntax
]>
<beans>
</beans>

2: Local DTD,DTD documents are used on the local system, within the company's own projects.
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE beans SYSTEM "BEAN.DTD" >
<beans>
</beans>

3: External dtd-Public DTD,DTD documents are available on the network and are generally provided by the framework.
A network is required and is cached locally after the first use.
<?xml version= "1.0" encoding= "UTF-8"?>
<!--the name of the DTD--
<! DOCTYPE beans Public "-//spring//dtd BEAN 2.0//en"
<!--DTD network location--
"Http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
<beans>

3.5 elements
<! Element element Name Description >
?: can appear, but only once
*: Allow multiple occurrences, or 0 times
+: The object appears at least once, but can be multiple times
,: Elements appear in the specified order

//------------------------------------------
root element: Beans and has two child elements: bean (0 or more) and import (0 or more times, and must appear in the specified order)
#PCDATA: Indicates that the element body is text

<! ELEMENT beans (bean*,import*) >
<! ELEMENT Bean (property*) >
<! ELEMENT Property (#PCDATA) >

<! ELEMENT import (#PCDATA) >

Bean has three properties: ID className type
<! ATTLIST Bean ID ID #REQUIRED
ClassName CDATA #REQUIRED
Type CDATA #IMPLIED
>

<! Attlist Property name CDATA #REQUIRED
Value CDATA #REQUIRED
>

<! Attlist Import resource CDATA #REQUIRED >


3.2 Schema constraints
Schema is the new XML document constraint;
Schemas are much more powerful than DTDs and are DTD substitutes;
The schema itself is an XML document, but the schema document's extension is XSD, not XML.
More powerful schemas and better data types
Schema Support namespaces
3.3 Constraining documents and XML relationships
When a Schema constraint specification is presented, the "Official binding document" is provided. With official documentation, we have to "Customize the schema constraint document", which is provided by the framework writer for the development of "custom documents". We provide a "custom document" qualification to write your own XML document.
3.3 Namespaces
The default namespace
Xmlns= "Http://www.itcast.cn/bean"
An explicit namespace
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Prefixes must be prefixed when using labels


4:xml parsing
is to take out the XML stored data (attributes and text)
<!--XML FILE--

Common APIs are as follows:
1.SaxReader objects
A) read (...) load execute XML document
2.Document objects
A) getrootelement () get root element
3.Element objects
A) elements (...) gets all child elements of the specified name. You can not specify a name
b) Element (...) Gets the first child element of the specified name. You can not specify a name
c) GetName () gets the element name of the current element
d) AttributeValue (...) Gets the property value of the specified property name
e) elementtext (...) Gets the text value of the specified name child element
f) GetText () Gets the text content of the current element

XML of Java Learning notes

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.