XML basics: structure and syntax
[Introduction] Now let's use Notepad to create our xml file. Let’s look at an XML file first: by Use "Notepad" to create our xml file. First look at an XML file:
Example 1
〈?xml version="1.0" encoding="gb2312" ?〉 〈参考资料〉 〈书籍〉 〈名称〉XML入门精解〈/名称〉 〈作者〉张三〈/作者〉 〈价格 货币单位="人民币"〉20.00〈/价格〉 〈/书籍〉 〈书籍〉 〈名称〉XML语法〈/名称〉 〈!--此书即将出版--〉 〈作者〉李四〈/作者〉 〈价格 货币单位="人民币"〉18.00〈/价格〉 〈/书籍〉 〈/参考资料〉
This is a typical XML file. After editing, it is saved as a file with the .xml suffix. We can divide this file into two major parts: the file preamble (PRolog) and the file body. The first line in this file is the file preamble. This line is something that an XML file must declare, and it must also be located on the first line of the XML file. It mainly tells the XML parser how to work. Among them, version is the standard version number used by this XML file, which is required; encoding specifies the character type used in this XML file, which can be omitted. When you omit this statement, the following character code must be Unicode Character code (it is recommended not to omit it). Because we are using GB2312 character code in this example, the encoding statement cannot be omitted. There are also some declaration statements in the preamble of the file, which we will introduce later.
The rest of the file belongs to the file body, and the content information of the XML file is stored here. We can see that the main body of the file is composed of the starting
〈!--This book will be published soon--〉This sentence is the same as HTML, it is a comment. In the XML file, the comment part is placed between the "〈!--" and "--〉" tags between parts.
As you can see, XML files are quite simple. Like HTML, XML files are also composed of a series of tags. However, the tags in XML files are our own custom tags and have clear meanings. We can explain the meaning of the content in the tags.
After having a preliminary impression of XML files, let’s talk about the syntax of XML files in detail. Before talking about grammar, we must understand an important concept, which is XML Parse.
1.XML parser
The main function of the parser is to check whether there are structural errors in the XML file, strip the tags in the XML file, and read out the correct content to pass to the next One-step application processing. XML is a markup language used to structure file information. The XML specification has detailed rules on how to mark the structure of files. The parser is software written according to these rules (mostly written in Java). Just like HTML, in the browser, there must be an HTML parser, so that the browser can "read" various web pages composed of HTML tags and display them in front of us. If there are tags that the browser's HTML parser cannot read, an error message will be returned to us.
Because the current HTML tags are actually quite confusing, and there are a lot of non-standard tags (some web pages can be displayed normally with IE, but not with Netscape Navigator), so from the beginning, XML designers The syntax and structure of XML are strictly stipulated. The XML files we write must comply with these regulations, otherwise the XML parser will show you error messages mercilessly.
There are two types of XML files, one is the Well-Formed XML file and the other is the Validating XML file.
If an XML file satisfies certain relevant rules in the XML specification and does not use DTD (document format definition - details later), it can be called Well-Formed. And if an XML file is Well-Formed, the DTD is used correctly, and the syntax in the DTD is correct, then the file is Validating. Corresponding to the two XML files, there are two XML parsers, one is the Well-Formed parser and the other is the Validating parser. IE 5 includes a Validating parser, which can also be used to parse Well-Formed XML files.
Check whether it meets the conditions of Well-Formed. We can open the first XML file we just edited with a browser of IE 5 or above.
You may ask why the display in the browser is the same as my source file? That's right, because for XML files, we only know the content, and its display form is completed by CSS or XSL. Here, we have not defined its CSS or XSL file for this XML file, so it is displayed in its original form. In fact, for electronic data interchange, only an XML file is needed. If we want to display it in some form, we must edit the CSS or XSL file (this issue will be discussed later).
2. Well-Formed XML file
We know that XML must be Well-Formed in order to be correctly parsed by the parser and displayed in the browser. So what is a Well-Formed XML file? There are mainly the following guidelines, which must be met when we create XML files.
First of all, the first line of the XML file must declare that the file is an XML file and the XML specification version it uses. There cannot be other elements or comments in front of the file.
Second, there can be only one root element in an XML file. In our first example, 〈References〉... 〈/References〉 is the root element of this XML file.
Third, the tags in the XML file must be closed correctly, that is, in the XML file, the control tag must have a corresponding closing tag. For example: the in HTML without an end tag, XML calls it an "empty element" and you must use The writing method is: 〈empty element name/〉. If the element contains attributes, the writing method is: 〈empty element name attribute name = "attribute value"/〉.
Fourth, marks must not cross. In the previous HTML file, you can write like this: 〈B〉〈H〉XXXXXXX〈/B〉〈/H〉, 〈B〉 and 〈H〉
There are Overlapping areas, but in XML, it is strictly prohibited to write such interleaved tags, and tags must appear in a regular order.
Fifth, the attribute value must be enclosed in " ". Such as "1.0", "gb2312", "RMB" in the first example. They are all enclosed by " " and cannot be missed.
Sixth, English control tags, instructions and attribute names must be case-sensitive. Unlike HTML, in HTML, tags like and have the same meaning, while in XML, tags like
Seventh, we know that in HTML files, if we want the browser to display the things we input intact, we can put these things in 〈pre〉〈/pre〉 or 〈 xmp〉〈/xmp〉marks the middle. This is essential for us to create HTML teaching web pages, because the source code of HTML must be displayed in the web page. In XML, to implement such a function, CDATA tags must be used. The information in the CDATA tag is passed intact to the application by the parser, and any control tags in the segment of information are not parsed. The CDATA area is marked by "## Example 2
〈
Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
