Home Backend Development XML/RSS Tutorial XML basics: structure and syntax

XML basics: structure and syntax

Mar 19, 2017 pm 03:40 PM
php php tutorial Video tutorial

[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〈/价格〉 
   〈/书籍〉 
  〈/参考资料〉
Copy after login



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 and the ending control tags. This is called the "root element" of the XML file; is the "root element" directly under the root element. "Sub-element"; under "Book" there are sub-elements such as "Name", "Author" and "Price". The currency unit is an "attribute" in the element, and "RMB" is the "attribute value".

 〈!--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 tag must have a corresponding closing tag. Unlike HTML, the closing tag of some tags is optional. If you encounter a self-contained unit tag in an XML file, which is similar to XML basics: structure and syntax 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 , , or are different.

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

 〈![CDATA[flying xml〉〉〉〉〉,:-)
oooo〈〈〈〈〈〈
 >〉

Eighth, XML handles whitespace characters differently from HTML. The HTML standard stipulates that no matter how many blanks there are, they will be treated as one blank; while in XML, it is stipulated that the parser must faithfully hand over all blanks other than tags to downstream applications for processing. In this way, we sometimes have to abandon the indentation habit when writing HTML files, because the parser also has to process the indented spaces. Such as:

         
        
          
        
       
       
       
       
       
       
       
       
        
       
       
       
       
       
       
       
       
       
       
        should be required is different (the latter includes in addition to the character Zhang San in the tag, it also includes two line break marks and the text indentation symbol before "Zhang San"). Therefore, the parser will have different processing results after removing the mark and passing the information to the application.

If we want to clearly tell the XML program that the spaces in the tags have a clear meaning and should not be removed casually (for example, in some poems, spaces have specific meanings), then we can add an XML tag to the tags Built-in attribute - xml:space. For example (note the capitalization of attribute names and values):  

  Poetry xml:space="preserver"〉
  My motherland! motherland!
My Homeland!
  /Poetry〉
### In addition, in the XML file, if the special characters in Table 1 are to be used, they must be replaced with corresponding symbols. ###### Table 1###### Special character substitution symbols### #### Let’s make a summary here: the XML file that meets the above requirements is the Well-Formed XML file. This is the most basic requirement for writing XML files. You can see that the syntax of XML files is much stricter than that of HTML. Due to such strict regulations, it is much easier for software engineers to write XML parsers. Unlike writing parsers for HTML language, they must work hard to adapt to different web page writing methods and improve the adaptability of their own browsers. In fact, this is also a good thing for us beginners. Just do what you want, and don't have to worry about how to write various HTMLs like before. ######We see that in XML files, most of the custom tags are used. But think about it, if two companies A and B in the same industry want to exchange data with each other using XML files, company A uses the tag to represent the price information of their products, while company B may use to represent the price. information. If an XML application reads the information in their respective XML files, if it only knows that the tag represents price information, then company B's price information cannot be read, and an error will occur. Obviously, for entities that want to use XML files to exchange information, there must be an agreement between them - that is, which tags can be used to write XML files, which sub-elements can be included in the parent element, the order in which each element appears, and the How to define the properties, etc. This way they can have smooth communication when exchanging data with XML. This convention is called DTD (Document Type Definition, document format definition). You can think of a DTD as a template for writing XML files. For XML data exchange between the same industry, it will be much more convenient to have a fixed DTD. For example, if the XML web pages of major electronic shopping malls on the Internet all follow the same DTD, then we can easily write an application based on this DTD to automatically capture the things we are interested in online. In fact, there are already several well-defined DTDs, such as MathML, SMIL, etc. mentioned earlier.

If an XML file is Well-Formed and it is correctly created based on a DTD, then the XML file is called: Validating XML file. The corresponding parser is called: Validating Parser.                    

The above is the detailed content of XML basics: structure and syntax. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles