A detailed introduction to XML structure and syntax
Now let's 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, save it 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 indicates the standard version number used in this XML file, which is required; encoding indicates the character type used in this XML file, which can be omitted. When you omit this statement, the following The character code must be a 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, the XML file is 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, it will return us an error message.
Since 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), the document 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.
1.XML文件的第一行必须是声明该文件是XML文件以及它所使用的XML规范版本。在文件的前面不能够有其它元素或者注释。
2.在XML文件中有且只能够有一个根元素。我们的第一个例子中,〈参考资料〉... 〈/参考资料〉就是此XML文件的根元素。
3.在XML文件中的标记必须正确地关闭,也就是说,在XML文件中,控制标记必 须有与之对应的结束标记。如:〈名称〉标记必须有对应的〈/名称〉结束标记,不像HTML,某些标记的结束标记可有可无。如果在XML文件中遇到自成一个单元的标记,就是类似于HTML 中的〈img src=.....〉的这些没有结束标记的时候,XML把它称为“空元素”,必须用这样的写法:〈空元素名/〉,如果元素中含有属性时写法则为:〈空元素名 属性名=“属性值”/〉。
4.标记之间不得交叉。在以前的HTML文件中,可以这样写:
〈B〉〈H〉XXXXXXX〈/B〉〈/H〉,〈B〉和〈H〉
标记之间有相互重叠的区域,而在XML中,是严格禁止这样标记交错的写法,标记必须以规则性的次序来出现。
5.属性值必须要用“ ”号括起来。如第一个例子中的“1.0”、“gb2312”、“人民币”。都是用“ ”号括起来了的,不能漏掉。
6.控制标记、指令和属性名称等英文要区分大小写。与HTML不同的是,在HTML中, 类似〈B〉和〈b〉的标记含义是一样的,而在XML中,类似〈name〉、〈NAME〉或〈Name〉这样的标记是不同的。
7.我们知道,在HTML文件中,如果我们要浏览器原封不动地将我们所输入的东西显示出来,可以将这些东西放到〈pre〉〈/pre〉或者〈xmp〉〈/xmp〉标记中间。这对于我们创建HTML教学的网页是必不可少的,因为网页中要显示HTML的源代码。而在XML中,要实现这样的功能,就必须使用CDATA标记。在CDATA标记中的信息被解析器原封不动地传给应用程序,并且不解析该段信息中的任何控制标记。CDATA区域是由:“〈
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

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

How to handle XML and JSON data formats in C# development requires specific code examples. In modern software development, XML and JSON are two widely used data formats. XML (Extensible Markup Language) is a markup language used to store and transmit data, while JSON (JavaScript Object Notation) is a lightweight data exchange format. In C# development, we often need to process and operate XML and JSON data. This article will focus on how to use C# to process these two data formats, and attach

Use PHPXML functions to process XML data: Parse XML data: simplexml_load_file() and simplexml_load_string() load XML files or strings. Access XML data: Use the properties and methods of the SimpleXML object to obtain element names, attribute values, and subelements. Modify XML data: add new elements and attributes using the addChild() and addAttribute() methods. Serialized XML data: The asXML() method converts a SimpleXML object into an XML string. Practical example: parse product feed XML, extract product information, transform and store it into a database.

Jackson is a Java-based library that is useful for converting Java objects to JSON and JSON to Java objects. JacksonAPI is faster than other APIs, requires less memory area, and is suitable for large objects. We use the writeValueAsString() method of the XmlMapper class to convert the POJO to XML format, and the corresponding POJO instance needs to be passed as a parameter to this method. Syntax publicStringwriteValueAsString(Objectvalue)throwsJsonProcessingExceptionExampleimp

Lambda expression is an anonymous function without a name, and its syntax is: (parameter_list)->expression. They feature anonymity, diversity, currying, and closure. In practical applications, Lambda expressions can be used to define functions concisely, such as the summation function sum_lambda=lambdax,y:x+y, and apply the map() function to the list to perform the summation operation.

The MySQL.proc table is a system table that stores stored procedure and function information in the MySQL database. By in-depth understanding of its structure and purpose, you can better understand the operating mechanism of stored procedures and functions in MySQL, and perform related management and optimization. The structure and purpose of the MySQL.proc table will be analyzed in detail below, and specific code examples will be provided. 1. The structure of the MySQL.proc table. The MySQL.proc table is a system table that stores the definitions and related information of all stored procedures and functions.

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

Introduction XML (Extensible Markup Language) is a popular format for storing and transmitting data. Parsing XML in Java is a necessary task for many applications, from data exchange to document processing. To parse XML efficiently, developers can use various Java libraries. This article will compare some of the most popular XML parsing libraries, focusing on their features, functionality, and performance to help developers make an informed choice. DOM (Document Object Model) parsing library JavaXMLDOMAPI: a standard DOM implementation provided by Oracle. It provides an object model that allows developers to access and manipulate XML documents. DocumentBuilderFactoryfactory=D
